package client;

import java.io.*;
import java.net.*;

public class ReceiveFile extends Thread 
{
	Socket skt;
	String filename;
	Client client;
	
	public ReceiveFile( String s, String host, Client c )
	{
		try{
			client = c;
			filename = "File\\" + s;
			skt = new Socket( InetAddress.getByName( host ), 4500 );
		}
		catch(IOException e) { 
            System.out.println(e.toString()); 
        }
	}
	
	public void run()
	{
		try{
			BufferedInputStream fileReader =
				new BufferedInputStream( skt.getInputStream() );
			BufferedOutputStream fileWriter = 
				new BufferedOutputStream( new FileOutputStream(filename) );
			int read;
			
			while( ( read = fileReader.read() ) != -1 )
			{ 
	            fileWriter.write( read ); 
	        }
			fileReader.close(); 
	        fileWriter.flush();
	        fileWriter.close();
	        skt.close();
	        client.showText( "\n" + filename + "ÀÉ®×±µ¦¬§¹²¦¡I\n", null );
	        client.setEndSelection();
		}
		catch(IOException e) { 
            System.out.println(e.toString()); 
        }
	}
}
