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

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.util.*; i public class TFTPserver extends Thread { p DatagramPacket sendPacket, receivePacket, tempPacket,ACKPacket, recveData; DatagramSocket commonSocket, sendReceiveSocket; static boolean terminate = false; //do not terminate the program byte [] ErrorData= null; 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);
Background image of page 1

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

View Full DocumentRight Arrow Icon
} //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.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; //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 connection between * server and client/clients */ public void run(){ //buffer to read data into BufferedInputStream readData = null; // buffer to write data from BufferedOutputStream writeData = null; /****** general initialization ******/ //size of the file can be adjusted here int filesize = 8388608; // for datapackets String opcode,filename,opCode,packet, eblock; // for ACK packets
Background image of page 2
String ackBlock, ackOpcode, ackpacket, ackop,read; //for receiving ACK byte [] receiveACK = new byte [4] ; String receiveACKNo; //for WRQ byte ACKpacket [] ; b //extracts data from tempPacket String packetInfo = new String(tempPacket.getData()); //get the first two bytes that is opcode
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.

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 / 10

TFTPserver - import java.io.*; import java.net.*; i 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