TFTPclient.java

TFTPclient.java - import java.io.*; import java.net.*;...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: import java.io.*; import java.net.*; import java.lang.Byte; import java.lang.Integer; public class TFTPclient { DatagramPacket sendPacket, receivePacket,dataACK,p; DatagramSocket sendReceiveSocket; BufferedReader input,filename,mode,simulator; String command,Namefile,firstOp,secondOp,modeType,message,simRun; BufferedInputStream readData = null; BufferedOutputStream writeData = null; String ACKpacket=null; byte fileData; byte ErrorData; String received=null; int port=69; public TFTPclient() { try { // Construct a datagram socket and bind it to any available // port on the local host machine. This socket will be used to // send and receive UDP Datagram packets. sendReceiveSocket = new DatagramSocket(); } catch (SocketException se) { // Can't create the socket. se.printStackTrace(); System.exit(1); } } public void start(){ command=""; while(true){ System.out.println("Enter your command: RRQ, WRQ, or Quit:"); input = new BufferedReader(new InputStreamReader(System.in)); try{ command = input.readLine(); }catch(IOException e){ } if(command.equalsIgnoreCase("Quit")){ sendReceiveSocket.close(); break; } if (!command.equalsIgnoreCase("RRQ")&&!command.equalsIgnoreCase("WRQ")&&!command.equalsIgnoreCase("Quit")){ System.out.println("not Valid: Run the Program again"); sendReceiveSocket.close(); break; } System.out.println("Enter the name of the file:"); filename = new BufferedReader(new InputStreamReader(System.in)); try{ Namefile = filename.readLine(); } catch(IOException e){ } if(command.equalsIgnoreCase("RRQ")){ firstOp = "0"; secondOp = "1"; } if(command.equalsIgnoreCase("WRQ")){ firstOp = "0"; secondOp = "2"; } System.out.println("Enter the mode required:"); System.out.println("Netascii or Octet"); mode = new BufferedReader(new InputStreamReader(System.in)); try{ modeType = mode.readLine(); }catch(IOException e){ } System.out.println("Do you want to test the simulator?(enter y or n) "); simulator = new BufferedReader(new InputStreamReader(System.in)); try{ simRun = simulator.readLine(); }catch(IOException e){ } if(simRun.equalsIgnoreCase("y")){ port=68; }else if(simRun.equalsIgnoreCase("n")){ port=69; } else { System.out.println("Enter is invalid."); System.exit(1); } message = "" + firstOp + secondOp + Namefile + firstOp + modeType + firstOp; byte msg = message.getBytes(); try { sendPacket = new DatagramPacket(msg, msg.length,InetAddress.getLocalHost(), port); } catch (UnknownHostException e) { e.printStackTrace(); System.exit(1); } if(command.equalsIgnoreCase("RRQ")){ readFile(); }else if(command.equalsIgnoreCase("WRQ")){ writeFile(); } } } public void readFile(){ System.out.println("Client: Sending packet:"); System.out.println("To host: " + sendPacket.getAddress()); System.out.println("Destination host port: " + sendPacket.getPort()); System.out.println("Length: " + sendPacket.getLength()); System.out.println(new String(sendPacket.getData())); // Send the datagram packet to the server via the send/receive socket. try { sendReceiveSocket.send(sendPacket); } catch (IOException e) { e.printStackTrace(); System.exit(1); } System.out.println("Client: Packet sent."); // Construct a DatagramPacket for receiving packets up // to 512 bytes long (the length of the byte array). //holds the data packet byte data1 = new byte [8388608]; receivePacket = new DatagramPacket(data1, data1.length); System.out.println("Client: Waiting for packet."); boolean done = false; int dummref=0; try{ writeData = new BufferedOutputStream(new FileOutputStream ("c:\\temp\\"+"c-"+Namefile)); }catch(Exception e){ System.out.println("Couldn't build a buffer"); } byte data = null; byte temp = null; while(!done){ /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// String d =new String( receivePacket.getData()); String errorOpcode = d.substring(0,2); String errorCode = d.substring(2,4); if(errorOpcode.equalsIgnoreCase("05")) { String errorMsg=new String (d.substring(4,(d.indexOf('0',4)))); if((errorCode.equalsIgnoreCase("01"))||(errorCode.equalsIgnoreCase("02"))|| (errorCode.equalsIgnoreCase("03"))||(errorCode.equalsIgnoreCase("00"))) { if(errorCode.equalsIgnoreCase("01")){ System.out.println("File not found"); System.out.println(errorMsg); break; }else if(errorCode.equalsIgnoreCase("02")){ System.out.println("Access violation"); System.out.println(errorMsg); break; }else if(errorCode.equalsIgnoreCase("03")){ System.out.println("Disk full or allocation exceeded"); System.out.println(errorMsg); break; }else if(errorCode.equalsIgnoreCase("00")){ System.out.println("Not defined, see error message"); System.out.println(errorMsg); break; } } } try { // Block until a datagram is received via sendReceiveSocket. sendReceiveSocket.receive(receivePacket); } catch(IOException e) { e.printStackTrace(); System.exit(1); } if(dummref==0){ temp=receivePacket.getData(); data= new byte[receivePacket.getLength()-4]; for(int i=4;i<receivePacket.getLength();i++)data[i-4]=temp[i]; dummref+=data.length; }else{ temp=new byte[data.length]; for(int i=0;i<data.length;i++)temp[i]=data[i]; data = new byte[data.length+receivePacket.getLength()-4]; for(int i=0;i<temp.length;i++)data[i]=temp[i]; temp=receivePacket.getData(); for(int i=4;i<receivePacket.getLength();i++)data[dummref+i-4]=temp[i]; dummref=data.length; } if(receivePacket.getLength()<516){ try{ writeData.write(data); done=true; }catch(Exception e){ try{ //ErrorData Error error=new Error (); String message="Could not write to the disk because it full"; ErrorData=error.form(3, message); System.out.println("Error 3 with message: "+message); sendReceiveSocket.send(new DatagramPacket (ErrorData, ErrorData.length, receivePacket.getAddress(), receivePacket.getPort())); sendReceiveSocket.close(); System.out.println("Error packet is sent and the connection is terminated"); return; }catch(Exception ex){ System.out.println("The packet could not be sent or the socket could not be closed"); } } } // sending An Ack String received = new String (temp); ACKpacket = new String("04"+received.substring(2,4)); System.out.println("send the server ACK: "+ACKpacket); byte ACK = new byte[512]; ACK = ACKpacket.getBytes();// convert message to bytes try { dataACK = new DatagramPacket(ACK, ACK.length, InetAddress.getLocalHost(), receivePacket.getPort()); } catch (UnknownHostException e) { e.printStackTrace(); System.exit(1); } try { sendReceiveSocket.send(dataACK); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } try{ writeData.close(); }catch(IOException e){ System.err.println("couldn't close the buffer"); } } public void writeFile(){ Reader rd = null; byte data1 = new byte [8388608]; receivePacket = new DatagramPacket(data1, data1.length); System.out.println("Client: Sending packet:"); System.out.println("To host: " + sendPacket.getAddress()); System.out.println("Destination host port: " + sendPacket.getPort()); System.out.println("Length: " + sendPacket.getLength()); System.out.println(new String(sendPacket.getData())); // Send the datagram packet to the server via the send/receive socket. try { sendReceiveSocket.send(sendPacket); } catch (IOException e) { e.printStackTrace(); System.exit(1); } System.out.println("Client: Packet sent."); try { // Block until a datagram is received via sendReceiveSocket. sendReceiveSocket.receive(receivePacket); } catch(IOException e) { e.printStackTrace(); System.exit(1); } String d =new String( receivePacket.getData()); String errorOpcode = d.substring(0,2); String errorCode = d.substring(2,4); if(errorOpcode.equalsIgnoreCase("05")){ String errorMsg=new String (d.substring(4,(d.indexOf('0',4)))); if(errorCode.equalsIgnoreCase("01")||errorCode.equalsIgnoreCase("02")|| errorCode.equalsIgnoreCase("03")||errorCode.equalsIgnoreCase("00")) { if(errorCode.equalsIgnoreCase("01")){ System.out.println("File not found"); System.out.println(errorMsg); sendReceiveSocket.close(); return; }else if(errorCode.equalsIgnoreCase("02")){ System.out.println("Access violation"); System.out.println(errorMsg); sendReceiveSocket.close(); return; }else if(errorCode.equalsIgnoreCase("03")){ System.out.println("Disk full or allocation exceeded"); System.out.println(errorMsg); sendReceiveSocket.close(); return; }else if(errorCode.equalsIgnoreCase("00")){ System.out.println("Not defined, see error message"); System.out.println(errorMsg); sendReceiveSocket.close(); return; } } } System.out.println("The file that the client is supposed to read from: "+Namefile); //03 | Block # | Data | String op,blocknum,dat; op="03"; try{ rd=new Reader(Namefile); }catch(FileNotFoundException e){ try{ Error error=new Error (); String message="The requested file does not exist at this directory"; ErrorData=error.form(1, message); System.out.println("Error 1 with message: "+message); sendReceiveSocket.send(new DatagramPacket (ErrorData, ErrorData.length, receivePacket.getAddress(), receivePacket.getPort())); sendReceiveSocket.close(); System.out.println("Error packet is sent and the connection is terminated"); return; }catch(Exception ex){ System.out.println("The packet could not be sent or the socket could not be closed"); } } byte fileData =null; int c=1; while (true){ try{ fileData=rd.Read(); }catch(IOException e){ Error error=new Error (); String message="You do not have read access to this directory"; ErrorData=error.form(2, message); System.out.println("Error 2 with message: "+message); try{ sendReceiveSocket.send(new DatagramPacket (ErrorData, ErrorData.length, receivePacket.getAddress(), receivePacket.getPort())); sendReceiveSocket.close(); System.out.println("Error packet is sent and the connection is terminated"); }catch(Exception ex){ System.out.println("error packet could not be sent or socket could not be closed"); } } if (c<10){ blocknum="0"+c; }else{ blocknum=""+c; } dat=new String (fileData); String r=op+blocknum; byte temp1=r.getBytes(); byte tftpData=new byte [fileData.length+4]; tftpData[0]=temp1[0]; tftpData[1]=temp1[1]; tftpData[2]=temp1[2]; tftpData[3]=temp1[3]; for (int i=0; i<fileData.length; i++){ tftpData[i+4]=fileData[i]; } p=new DatagramPacket (tftpData, tftpData.length, receivePacket.getAddress(), receivePacket.getPort()); try { sendReceiveSocket.send(p); } catch (IOException e) { e.printStackTrace(); System.exit(1); } System.out.println("sent a data packet from file: "+ Namefile); System.out.println("to server: " + p.getAddress()); System.out.println("server port: " + p.getPort()); System.out.println("Length: " + p.getLength()); System.out.println("block #: " + blocknum); try { // Block until a datagram is received via sendReceiveSocket. sendReceiveSocket.receive(receivePacket); } catch(IOException e) { e.printStackTrace(); System.exit(1); } try{ String t =new String( receivePacket.getData()); errorOpcode = t.substring(0,2); errorCode = t.substring(2,4); if(errorOpcode.equalsIgnoreCase("05")){ if(errorCode.equalsIgnoreCase("01")||errorCode.equalsIgnoreCase("02")|| errorCode.equalsIgnoreCase("03")||errorCode.equalsIgnoreCase("00")) { String errorMsg=new String (t.substring(4,(t.indexOf('0',4)))); if(errorCode.equalsIgnoreCase("01")){ System.out.println("File not found"); System.out.println(errorMsg); sendReceiveSocket.close(); return; }else if(errorCode.equalsIgnoreCase("02")){ System.out.println("Access violation"); System.out.println(errorMsg); sendReceiveSocket.close(); return; }else if(errorCode.equalsIgnoreCase("03")){ System.out.println("Disk full or allocation exceeded"); System.out.println(errorMsg); sendReceiveSocket.close(); return; }else if(errorCode.equalsIgnoreCase("00")){ System.out.println("Not defined, see error message"); System.out.println(errorMsg); sendReceiveSocket.close(); return; } } } System.out.println("received a packet"); System.out.println("From server: " + receivePacket.getAddress()); System.out.println("server port: " + receivePacket.getPort()); System.out.println("Length: " + receivePacket.getLength()); byte buffer = receivePacket.getData(); String de="" ; de =new String( buffer); String j=new String(de.substring(2,4)); String k=new String(de.substring(0,2)); if (!k.equals("04")|| !j.equals(blocknum)){ //check if its not an ACK packet System.out.println("did not receive a correct ack packet so terminate"); System.exit(1); } System.out.println("received the ack packet with block #: "+blocknum ); }catch(Exception e){ System.out.println("receiving ack problem..."); } if(fileData.length<512){ break; } if (c==99){ c=0; } c++; System.out.println("The reading process from "+ Namefile+" at"); System.out.println("the client is complete and all the data was sent"); }sendReceiveSocket.close(); } public static void main(String args) { TFTPclient c = new TFTPclient(); c.start(); try { Thread.sleep(10000); // so we can read the output } catch (InterruptedException e) {} } } ...
View Full Document

This note was uploaded on 09/26/2009 for the course SCE Sysc3303 taught by Professor Marshall during the Summer '09 term at Carleton CA.

Ask a homework question - tutors are online