TFTPclient - import import import i import java.io java.net...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
import java.io.*; import java.net.*; import java.lang.Byte; import java.lang.Integer; i public class TFTPclient { p 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(); c }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{
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Namefile = filename.readLine(); } catch(IOException e){ } if(command.equalsIgnoreCase("RRQ")){ i firstOp = "0"; secondOp = "1"; s } if(command.equalsIgnoreCase("WRQ")){ i 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(); } } }
Image of page 2
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.
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern