package client;

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

public class SendFile extends Thread
{
	Socket skt;
	PrintStream p;
	BufferedInputStream fileReader;
	Client client;
	File file;
	
	public SendFile( File f, String host, Client c )
	{
		try{
			client = c;
			file = f;
			skt = new Socket( InetAddress.getByName( host ), 4500 );
			p = new PrintStream( skt.getOutputStream() );
			fileReader = new BufferedInputStream( new FileInputStream( file ));
			client.showText( "\n¶Ç°e" + file.getName() + "\n", null );
			client.setEndSelection();
		}
		catch(IOException e) { 
            System.out.println(e.toString()); 
        }
	}
	public void run()
	{
		try{
			int read;
			
			while( ( read = fileReader.read() ) != -1 ) {
//				control.showText("." );
				p.write( read );
			}
			fileReader.close();
			p.flush();
			p.close();
			skt.close();
            client.showText("\nÀÉ®×" + file.getName() + "¶Ç°e§¹²¦¡I\n", null );
            client.setEndSelection();
		}
		catch(IOException e) { 
            System.out.println(e.toString()); 
        }
	}

}
