You are on page 1of 6

package receiverPackage; import import import import import;;

DatagramSocket; java.util.Arrays; java.util.Random;

public class ServerV01 { //MPS final static int MPS = 30; //Total Data Size final static int TOT_DAT = 500; //Size of the array to be sent final static int SENT_ARRAY_SIZE = 8; //Server Port Number final static int SRVR_PORT_NUMBR = 9999; //Location of MSB of Packet Type final static int LOC_PKT_TYP_MSB = 0; //Location of LSB of Packet Type final static int LOC_PKT_TYP_LSB = 1; //Location of MSB of Acknowledgement Number final static int LOC_ACK_NUM_MSB = 2; //Location of LSB of Acknowledgement Number final static int LOC_ACK_NUM_LSB = 3; //Location of MSB of checksum final static int LOC_CHECKSUM_MSB = 4; //Location of LSB of checksum final static int LOC_CHECKSUM_LSB = 5; //Location of MSB of RBS final static int LOC_RBS_MSB = 6; //Location of LSB of RBS final static int LOC_RBS_LSB = 7; //Location of MSB of Sequence Number final static int LOC_SEQ_NUM_MSB = 2; //Location of LSB of Sequence Number final static int LOC_SEQ_NUM_LSB = 3; //Location of MSB of Length final static int LOC_LEN_MSB = 6; //Location of LSB of Length final static int LOC_LEN_LSB = 7; //Initial Acknowledgement final static int INIT_ACK_NUM = 0; /** * @param args * @throws IOException */ @SuppressWarnings("resource") public static void main(String[] args) throws IOException { // TODO Auto-generated method stub //initialize Received Buffer Size to MPS int rbs = MPS; //sentArray -> sent data buffer; receivedArray -> received data buffer; dataArray -> Array of all received data bytes so far byte[] sentArray = new byte[SENT_ARRAY_SIZE], receivedArray = ne

(byte)0).getAddress().out. //calculate checksum of received packet receivedChecksum = calculateChecksum(receivedArray). //TEST CODE SEGMENT//System. //If received checksum is 0. dataArray = new byte[TOT_DAT]. . udpReceived = new DatagramPacket(receive dArray. //server socket. lsbByte. //receive packet from client serverSocket.receive(udpReceived). listening at port number SRVR_PORT_NUMBR DatagramSocket serverSocket = new DatagramSocket(SRVR_PORT_NUMB R). ackNumLsb. rbsLsb.fill(receivedArray.length . lengthLsb -> LSB of length. udpReceived. //keep doing the things until Server is manually turned off while(true){ //Fill array with zeros to make sure bytes from the prev iously received array do not remain Arrays. ackNumLsb = (byte) (ackNum & 0xff). lsbByte -> Byte form of LSB //ackNumMsb -> MSB of Ack Number. //Get MSB and LSB of ackNum ackNumMsb = (byte) ((ackNum>>8) & 0xff). subCounter.getPort() ). lengthLsb. recvArra yLength = receivedArray. sentArray[LOC_PKT_TYP_MSB] = (byte) ((pktType>>8 ) &0xff). //msbByte -> Byte form of MSB. sentChecksum -> Checksum of sent(Ack) packet long length. sentArray[LOC_PKT_TYP_LSB] = (byte) (pktType & 0 xff). //newRbs -> new random RBS Random newRbs = new Random().receivedChecksum). recvArrayLength). received packet type is 000 0 OR 0001.w byte[rbs]. subCounter -> counters. ackNum=INIT_ACK_NUM. recvArraylength -> Length(not size!) of the array received //ackNum -> Acknowledgement Number int counter. rbsLsb -> LSB of RBS byte msbByte. //length -> Length field of Received Packet //receivedChecksum -> Checksum of received packet.printf("\nThis is the ch ecksum Verification %x\n". rbsMsb -> MSB of RBS.getLength()<=rbs){ //Build UDP Packet //Packet Type pktType = 0x0002. lengthMsb.length. sentChecksum. udpReceived. //Initialize udpSent udpSent = new DatagramPacket(sentArray. //lebgthMsb -> MSB of length. receivedChecksum. ackNumLsb -> LSB of Ack Number . ackNumMsb. seqNum=ackNum and length of received packet < RBS if((short)receivedChecksum==0x0){ if (receivedArray[LOC_PKT_TYP_MSB]==0x00 && (rec eivedArray[LOC_PKT_TYP_LSB]==0x00||receivedArray[LOC_PKT_TYP_LSB]==0x01) && rece ivedArray[LOC_SEQ_NUM_MSB]==ackNumMsb && receivedArray[LOC_SEQ_NUM_LSB]==ackNumL sb && udpReceived. sentArray. pktType. //send and receive UDP packets respectively DatagramPacket udpSent. rbsMsb. pktTyp e -> Packet Type //counter.

ackNum. lengthLsb = lsbByte>=0? lsbByte:lsbByte+ 256. (int) length). . //TEST CODE SEGMENT//Sys tem.out. //RBS //Generate a new RBS between MPS/2 and M PS rbs = (MPS/2)+newRbs. ackNumLsb = (byte) (ackNum & 0xff).println(dataArray[counter]). dataArray. 8. counter<ackNum+lengt h. rbsMsb = (byte) ((rbs>>8) & 0xff).out. ackNumMsb = (byte) ((ackNum>>8) & 0xff). for(subCounter =8. println(ackNum). sentArray[LOC_CHECKSUM_LSB] = 0. //Checksum //Set Checksum field to zero sentArray[LOC_CHECKSUM_MSB] = 0. //put msb & lsb together to form the len gth length = (lengthMsb<<8) + lengthLsb.out. lsbByte = receivedArray[LOC_LEN_LSB]. counter++){ //TEST CODE SEGMENT//System. sentArray[LOC_ACK_NUM_LSB] = ackNumLsb. sentArray[LOC_RBS_MSB]=rbsMsb.subCounter++){ System.arraycopy(receive dArray.nextInt((MPS/2)+1).printf(" \nThis is the checksum Calculated+++++++++++++++++++++++++ %x\n". } }} //Increment ACK Number ackNum += length.//Acknowledgement Number //Put together MSB and LSB of Length fie ld //calculate the msb & lsb in byte form msbByte = receivedArray[LOC_LEN_MSB].subCounter<udp Received. sentArray[LOC_RBS_LSB]=rbsLsb.getLength(). //convert the byte forms of msb & lsb in to int in order to remove the sign bit lengthMsb = msbByte>=0? msbByte:msbByte+ 256.sentChecksum). rbsLsb = (byte) (rbs & 0xff). //Store the received array in the dataAr ray if(ackNum<TOT_DAT){ for(counter=ackNum. //TEST CODE SEGMENT//System. sentArray[LOC_ACK_NUM_MSB] = ackNumMsb. sentChecksum = calculateChecksum(sentArr ay).

println("\n******SENT PKT******").out.send(udpSent). ackNum=INIT_ACK_NUM. } } } else System. //send packet to client serverSocket.println("\n****************").out. Problem!!!\ n").idx<TOT_DAT.idx++ ) { if(idx%10==0){ System.idx++) { if(idx%25==0) System. } else System.println().printf("\nChecksum Problem!!!\n").println("\n****************** ********************************************************\n\n\n\nTHE RECEIVED BYT ES ARE:"). } System. lsb -> MSB & LSB with sign bit removed int counter. //Print 25 bytes per line of out put for(int idx =0.length. sentArray[LOC_CHECKSUM_LSB] = (byte)(sen tChecksum&0xff). msb.*/ }//while }//main private static long calculateChecksum(byte[] Array) { // TODO Auto-generated method stub //counter -> counter to calculate sum. //TEST CODE SEGMENT /*System.idx<sentArray. System. msb. ".out. //Reset rbs and ackNum so that client can be run multiple number of times during one run of the server if(ackNum==500){ rbs = MPS.out. } System.sentArray[LOC_CHECKSUM_MSB] = (byte)((se ntChecksum>>8) & 0xff).printf("%x.printf("\nACK No.out.sentArray[idx]).out.out.dataArray[idx]). for(int idx =0.out.printf("%x\t" . .out. lsb.print ln("\n"). //Print the received packet System.

add sum with the shifted sum //this adds carries gene rated while calculating 1s complement sum while(sum >> 16 != 0) sum = (sum >> 16 ) + (sum & 0xffff). counter++) { //calculate the msb & lsb in byte form msbByte = Array[ 2*counter].out.out.printf("%x.out.length/2. ". for(int id { if(id%25==0) System. //convert the by te forms of msb & lsb into int in order to remove the sign bit msb = msbByte>=0 ? msbByte:msbByte+256.println("^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"+Array. System. lsbByte. //put msb & lsb together to form the sum sum += (msb<<8) + lsb. //checksum is the 1s com plement of the 1s complement sum checksum = ~sum. } //If the length of the array is odd .println("\n******Checksum PKT******") . checksum. lsbByte = Array[ 2*counter+1].out. return checksum. System.out.length%2==0) { for(counter = 0. //TEST CODE SEGM ENT//<Array. } //while the shifted sum results in a 1 (sign bit).Array[id]).println("\n****************").length). long sum = 0. } System.*/ //As long as the length of the array is even if(Array. lsb = lsbByte>=0 ? lsbByte:lsbByte+256. // msb & lsb in byte form byte msbByte.out. //calculate checksum //TEST CODE SEGMENT /*System.length. counter <Array.//sum -> unflipped checksum.println("DOING DOING "+lsbByte).println().

length+1].counter<Ar ray. Original array is unaffected return calculateChecksum (tempArray).length. for(counter=0. //Recursive call to calc ulate checksum of the zero-padded array.else{ //Copy the array into a temporary array and pad a zero. } } }//calculateChecksum . thus making it even byte[] tempArray = new b yte[Array.counter++) tempArray[counte r] = Array[counter]. tempArray[counter+1] = 0 .