TFTPserver.java

TFTPserver.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
import java.io.*; import java.net.*; import java.util.*; public clas TFTPserver extends Thread { DatagramPacket sendPacket, receivePacket, tempPacket,ACKPacket, recveData; DatagramSocket commonSocket, sendReceiveSocket; static bo lean terminate = false; //do not terminate the program byte [] Er orData= nul ; Er or error=new Error (); static int counter = 0 ; /* * Constructs the com on socket 69 */ public TFTPserver() { try{ com onSocket = new DatagramSocket(69); } catch (SocketException se){ se.printStackTrace(); System.exit(1); } } /* * This method reads packets from port 69 * and if it is a R Q or WRQ returns true * otherwise the data is invalid and returns * false. close con ection if client requested a quit but ke p server run ing. */ public bo lean validateRequest(){ / this ar ay wil contain the message from received packet byte request [] = new byte [516]; / forming a socket to receive the W/R Q receivePacket = new DatagramPacket(request, request.length); try{ / this wil put the data in receivePacket into request[] com onSocket.receive(receivePacket); }catch(IOException e){ System.out.println("Validation failed"); System.exit(1); } / extract data from packet request = receivePacket.getData(); String received = new String(request); / get the first two bytes that contain opcode String opcode = new String(received.substring(0,2) ; System.out.println("received packet containing :" + received); System.out.println("from host: " + receivePacket.getAd res () ; System.out.println("host port: " + receivePacket.getPort() ; System.out.println("Length: " + receivePacket.getLength() ; / store the packet into tempPacket to / be ac es ed by run() tempPacket = receivePacket; / return true for read or write request if(opcode.equalsIgnoreCase("01")| opcode.equalsIgnoreCase("02") { System.out.println("Request is valid."); return true; / return false for opcode other than read or write }else { System.out.println("Request is invalid."); return false; } } / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /* * This method is responsible for * establishing a con ection betwe n * server and client/clients */ public void run(){ / buf er to read data into Buf eredInputStream readData = null; / buf er to write data from Buf eredOutputStream writeData = nul ; /* ** * general initialization * * * / / size of the file can be adjusted here int filesize = 838 608; / for datapackets String opcode,filename,opCode,packet, eblock; / for ACK packets String ackBlock, ackOpcode, ackpacket, ackop,read; / for receiving ACK byte [] receiveACK = new byte [4] ; String receiveACKNo; / for WRQ byte ACKpacket [] ; / extracts data from tempPacket String packetInfo = new String(tempPacket.getData() ; / get the first two bytes that is opcode
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.
Ask a homework question - tutors are online