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

Info iconThis 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; } 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{
Background image of page 1

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

View Full DocumentRight 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(); } } }
Background 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. try {
Background image of page 3

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

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

Page1 / 11

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

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online