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

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight 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.util.*; i public class TFTPserver extends Thread { p DatagramPacket sendPacket, receivePacket, tempPacket,emPacket,ACKPacket, recveData; DatagramSocket commonSocket, sendReceiveSocket; static boolean terminate = false; //do not terminate the program byte ErrorData= null; byte emptyPacket = new byte[0]; Error error=new Error (); E static int counter = 0 ; s /* * Constructs the common socket 69 */ public TFTPserver() { try{ commonSocket = new DatagramSocket(69); } catch (SocketException se){ se.printStackTrace(); System.exit(1); } } /* * This method reads packets from port 69 * and if it is a RRQ or WRQ returns true * otherwise the data is invalid and returns * false. close connection if client requested a quit but keep server running. */ public boolean validateRequest(){ // this array will contain the message from received packet byte request = new byte [516]; // forming a socket to receive the W/RRQ receivePacket = new DatagramPacket(request, request.length); try{ // this will put the data in receivePacket into request commonSocket.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); System.out.println("Received="+received); //get the first two bytes that contain opcode String opcode = new String(received.substring(0,2)); System.out.println("opcode" + "" + opcode); String filename = new String(received.substring(2,received.indexOf('0',1))); System.out.println("here3"); int temp1 = filename.length(); temp1 += 3; String mode = new String (received.substring(temp1,received.indexOf('0',temp1))); System.out.println("received packet containing :" + received); System.out.println("from host: " + receivePacket.getAddress()); System.out.println("host port: " + receivePacket.getPort()); System.out.println("Length: " + receivePacket.getLength()); // store the packet into tempPacket to // be accessed by run() tempPacket = receivePacket; System.out.println("Before the if"); S //return true for read or write request if(opcode.equalsIgnoreCase("01")||opcode.equalsIgnoreCase("02")){ System.out.println("After the if"); System.out.println(mode); if(mode.equalsIgnoreCase("OCTET")||mode.equalsIgnoreCase("NETASCII")){ System.out.println("Request is valid."); return true; }//SECOND IF //return false for opcode other than read or write / }if(!mode.equalsIgnoreCase("OCTET")||!mode.equalsIgnoreCase("NETASCII") || !opcode.equalsIgnoreCase("01")||!opcode.equalsIgnoreCase("02")){ System.out.println("Error occured"); byte requestError =null; requestError = error.form(4,"The request is invalid from error packet"); try{ sendReceiveSocket = new DatagramSocket(); }catch(SocketException se){ se.printStackTrace(); System.exit(1); } try{ sendReceiveSocket.send(new DatagramPacket (requestError, requestError.length, receivePacket.getAddress(), receivePacket.getPort())); sendReceiveSocket.close(); }catch(Exception e){} System.out.println("Request is invalid.");System....
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.

Page1 / 12

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

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