package ignisftpv20; import java.net.*; import java.io.*; import javax.swing.JOptionPane; public class FTPClient implements Runnable { ObjectInputStream input; ObjectOutputStream output; Socket s; FileInputStream fis; FileOutputStream fout; File file; public FTPClient(){ } public FTPClient(int port){ connectToServer(port); setIOStreams(); new Thread( this ).start(); } //connect to the server public void connectToServer(int port){ try{ s = new Socket ("127.0.0.1" , port); }catch(Exception e){e.printStackTrace();} } //set IO streams public void setIOStreams(){ try{ output = new ObjectOutputStream(s.getOutputStream()); output.flush(); // flush output buffer to send header information input = new ObjectInputStream(s.getInputStream()); JOptionPane.showMessageDialog(null, "Client says: ... SetIO Streams"); }catch(Exception e){e.printStackTrace();} } //send bytes to server public void sendBytes(File f){ try{ JOptionPane.showMessageDialog(null, "Client says: File send ...."); fis = new FileInputStream(f); JOptionPane.showMessageDialog(null, "Client says: The file name: " + f.getName()); byte[] buffer = new byte[1024]; int bytes = 0; while((bytes = fis.read(buffer))!=-1) { output.write(buffer,0,bytes); } }catch(Exception e){e.printStackTrace();} } //fos = FileOutputStream //o = objIn.readObject(); //while (o instanceof Byte) { fos.write(((Byte) o).byteValue()); //o = objIn.readObject(); //} //////////////////////////////////////////////////////////////////////////////// // CLIENT READS Incoming .... // this starts in a background thread public void run() { try { //reads incoming bytes forever while (true) { try { //Testing only String message = ( String ) input.readObject(); JOptionPane.showMessageDialog(null, message); } //end try catch(ClassNotFoundException e){e.printStackTrace(); //displayMessage( "\nOops, Alien object type received" ); } // end catch }//end while }//end try catch( IOException ie ) { System.out.println( ie ); } }// end of run //////////////////////////////////////////////////////////////////////////////// }