import java.io.*; import java.net.*; import java.awt.*; import java.util.*; public class Ftpclient extends Thread { static int keepgoing = 0; DatagramSocket dataSock; public static String filename; public static void main(String[] args) throws Exception { //TCP connection variables: Socket clientSocket = null; DataInputStream is = null; DataOutputStream outToServer = null; BufferedReader inFromUser = null; BufferedReader inFromServer = null; String host = null; int port, i, code; char[] line; String instring; host = new String(args[0]); port = 5180; // //loggedin = 1 if user logged in or loggedin = 0 if user not logged in int loggedin = 0; // //retrieve variables: long before=System.currentTimeMillis(); //gets the time before the transfer begins int MAX_WIRE_LENGTH = 16000; int datalength = 9; DatagramPacket packet = null; DatagramSocket dataSock = null; String filetoRead = null; // //store variables: String filetoWrite = null; int availableBytes = 1; // try {//try 1 //open streams and socket for TCP connection inFromUser = new BufferedReader(new InputStreamReader(System.in)); clientSocket = new Socket(host, port); outToServer = new DataOutputStream(clientSocket.getOutputStream()); inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); }//end try 1 catch (UnknownHostException e) { System.err.println("Don't know about host: " + host); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: hostname"); } try {//try 2 String responseLine; //variable used to store the line the server responds to the client with responseLine = inFromServer.readLine(); //read the response line from the server System.out.println("Server: " + responseLine); while(true) //executes FTP commands until the user quits { //while 1 instring = inFromUser.readLine(); //read the FTP command the client enters outToServer.writeBytes(instring + '\n'); //send the command to the server responseLine = inFromServer.readLine(); //read the servers respnse line //retrieve code---works only if user is logged in if((instring.substring(0,4).equals("RETR")) && loggedin == 1) { filename = instring.substring(5, instring.length()); //read the file the client wishes to retrieve dataSock = new DatagramSocket(3500); //create datagram socket for udp connection Ftpclient udpin = new Ftpclient(dataSock); //dataSock variable of class Ftpclient udpin.start(); //execute run method responseLine = inFromServer.readLine(); responseLine = inFromServer.readLine(); udpin.stop(); //stop the run method //if file doesn't exist erase it from directory if(responseLine.equals("450")) { System.out.println("The file requested does not exist!"); File deleteFile = new File(filename); deleteFile.delete(); } dataSock.close(); // close data sock }// end of retrieve command //break out of while loop if the client types QUIT if (instring.equals("QUIT")) { break; } //store code if(instring.substring(0,4).equals("STOR")) { filetoWrite = new String(instring.substring(5, instring.length())); //get file from the client and store in filetoWrite try { InetAddress addr = clientSocket.getInetAddress(); DatagramSocket outNet = new DatagramSocket(); //create datagram socket for udp connection //Open the File And Send it FileInputStream iStream = new FileInputStream(filetoWrite); System.out.println("Now Sending: " + filetoWrite); availableBytes = iStream.available(); //get # of available bytes Thread.sleep(1000); // pause program for 1 second to allow the program receiving data to get ready while(availableBytes > 0) //while there is still available bytes send the file to server { //create datagram packet called message and send the message to the server if(availableBytes <= 16000) { byte [] bytes = new byte [availableBytes]; int j = iStream.read(bytes); DatagramPacket message = new DatagramPacket(bytes, bytes.length, addr, 3500); outNet.send(message); } else { byte [] bytes = new byte [16000]; int j = iStream.read(bytes); DatagramPacket message = new DatagramPacket(bytes, bytes.length, addr, 3500); outNet.send(message); } //find the new value of available bytes availableBytes = iStream.available(); } outToServer.writeBytes("DONE" + '\n'); //let the server know the client is done transfering the file } catch (IOException e) { outToServer.writeBytes("NE" + '\n'); System.out.println("The file does not exist!"); } responseLine = inFromServer.readLine(); }// end of STORE ////If statements to determine definition for codes of which the server responds ////Read print statement to find definition of each code if(responseLine.equals("221")) { System.out.println( responseLine + " " + " Service closing control connection." ); System.out.println( " Logged out if appropriate." ); } else if(responseLine.equals("230")) { System.out.println( responseLine + " " + " User logged in proceed." ); loggedin = 1; //server logged in user therefor set loggedin to 1 } else if(responseLine.equals("150")) { System.out.println( responseLine + " " + " File status OK - about to open file." ); } else if(responseLine.equals("530")) { System.out.println( responseLine + " " + " Not logged in." ); loggedin = 0; //server did not log in user therefore set loggedin to 0 } else if(responseLine.equals("331")) { System.out.println( responseLine + " " + " User name okay, need password." ); loggedin = 0; //server needs a password for user name therefore not logged in and loggedin is 0 } else if(responseLine.equals("250")) { System.out.println( responseLine + " " + " Requested file action okay, completed."); } else if(responseLine.equals("450")) { System.out.println( responseLine + " " + " Requested file action not taken."); } else if(responseLine.equals("226")) { System.out.println( responseLine + " " + " Closing data connection. \n Requested file action successful."); } else if(responseLine.equals("500")) { System.out.println( responseLine + " " + " Syntax error, command unrecognized."); } else if(responseLine.equals("501")) { System.out.println( responseLine + " " + " Syntax error in parameters or arguments."); } else if(responseLine.equals("425")) { System.out.println( responseLine + " " + " Can't open data connection."); } else if(responseLine.equals("550")) { System.out.println( responseLine + " " + " Requested action not taken."); } else if(responseLine.substring(0, 3).equals("999")) { System.out.println( responseLine.substring(0, 3) + " " + responseLine.substring(4, responseLine.length())); } else if(responseLine.substring(0, 3).equals("998")) { System.out.println( responseLine.substring(0, 3) + " " + responseLine.substring(4, responseLine.length())); } else { System.out.println( " Invalid Command" ); } }// while 1 clientSocket.close(); }//try 2 catch (UnknownHostException e) { System.err.println("Trying to connect to unknown host: " + e);} catch (IOException e) { System.err.println("IOException: " + e);} }// main // public Ftpclient(DatagramSocket dataSock) { this.dataSock = dataSock; } public void run() { long before=System.currentTimeMillis(); //gets the time before the transfer begins int datalength = 9; DatagramPacket packet = new DatagramPacket(new byte[16000], 16000); //create datagram packet try { System.out.println("Retrieving " + filename); FileOutputStream outtoFile = new FileOutputStream(filename); datalength = 9; while(true) { dataSock.receive(packet); //retrieve the data sock packet datalength = packet.getLength(); //get the length of the packet byte [] theData = new byte [datalength]; //create byte array with size datalength byte [] packetBuf = new byte [16000]; //create byte array with size 16000 packetBuf = packet.getData(); //get data from packet and store in packetBuf //if data is equal to 16000 send the packetBuf to the server if (datalength == 16000) { outtoFile.write(packetBuf); } //otherwise get the data from each byte of packetBuf up to datalength and send to sever else { for(int i=0;i