package ServerPackage;
import java.io.*;
import java.net.*;

import javax.sound.sampled.AudioFormat.Encoding;



public class ServerClass {

	/**
	 * @param args
	 */
	
	static ServerSocket providerSocket;
	Socket connection = null;
	//ObjectOutputStream 
	BufferedOutputStream SocketOut;
	BufferedInputStream SocketIn;
	BufferedOutputStream FileOut = null;
	BufferedInputStream FileIn = null;
	String message;
	ServerClass(){}
	void run() 
	{
		try{

			//1. Creating the server socket
			providerSocket = new ServerSocket(1000, 10);
			System.out.println("Server started...");
			
			//2. Waiting for connection
			System.out.println("Waiting for a connection");
			connection = providerSocket.accept();
			System.out.println("Connection received from " + connection.getInetAddress().getHostName());
			
			//3. getting Socket Buffered Input and Output Streams
			SocketOut = new BufferedOutputStream(connection.getOutputStream());
			//SocketOut.flush();
			SocketIn = new BufferedInputStream(connection.getInputStream());
			
			// Receiving the File
			byte[] FileSize = new byte[8];
			int BytesRead = 0, offset = 0, i =0;
			FileOut = new BufferedOutputStream(new FileOutputStream("/tmp/EchoFile.txt")); // Check path for Linux
			
			
			BytesRead = SocketIn.read(FileSize); // Receive file size
			long FileLength = 0;
			for (int j = 0; j < FileSize.length; j++)
			{
				FileLength += ((long) FileSize[j] & 0xffL) << (8 * j);
			}
			System.out.println(FileLength);
			
			byte[] FileContent = new byte[50 * 1024 * 1024];
			while( offset < FileLength)
			{
				BytesRead = SocketIn.read(FileContent);
				FileOut.write(FileContent, offset, BytesRead);
				offset += BytesRead;
				FileOut.flush();
				System.out.println(i++);
				System.out.println(offset);
				//System.out.println(FileLength);
            }
			System.out.println("File Received...");
			FileOut.flush();
			FileOut.close();
			
			// Sending Back the File
			int _BytesRead = 0;
			int _offset = 0;
			byte[] _FileContent = new byte[(int)FileLength];
			//SocketOut.flush();
			// getting Socket Buffered Input and Output Streams
			//SocketOut = new BufferedOutputStream(connection.getOutputStream());
			//SocketOut.flush();
			//SocketIn = new BufferedInputStream(connection.getInputStream());
			FileIn = new BufferedInputStream(new FileInputStream("/tmp/EchoFile.txt")); // Check path for Linux
			while((_BytesRead = FileIn.read(_FileContent)) > 0)
			{
				SocketOut.write(_FileContent, _offset, _BytesRead);
				_offset += _BytesRead;
				SocketOut.flush();
			}
			System.out.println("File Sent Back Again to Client...");
			
			SocketIn.close();
			SocketOut.flush();
			SocketOut.close();
			//FileOut.flush();
			//FileOut.close();
			providerSocket.close();
			
			
			//SocketIn.close();
			//SocketOut.flush();
			//SocketOut.close();
			//FileIn.close();
			//providerSocket.close();
			
		}
		catch(IOException ioException){
			ioException.printStackTrace();
		} 
		
		/*catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		//finally
		//{
			//4: Closing connection
			//try{
				//in.close();
			//	out.flush();
				//out.close();
				//FileOut.flush();
				//FileOut.close();
				//FileIn.close();
				//providerSocket.close();
			//}
			//catch(IOException ioException){
			//	ioException.printStackTrace();
			//}
		}
	
	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
					
		ServerClass server = new ServerClass();
		while(true){
					server.run();}
		
	}

}