You are on page 1of 7

package transmitterPackage; import import import import import java.io.IOException; java.net.DatagramPacket; java.net.

DatagramSocket; java.net.InetAddress; java.util.*;

public class ClientV01 { //MPS final static int MPS = 30; //Total Data Size final static int TOT_DAT = 500; //Server Port Number final static int SRV_PRT_NUM = 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 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 checksum final static int LOC_CHECKSUM_MSB = 4; //Location of LSB of checksum final static int LOC_CHECKSUM_LSB = 5; //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 Sequence Number final static int INIT_SEQ_NUM = 0x0000; //Initial timeout duration final static int INIT_TIMEOUT = 1000; //Length of expected ACK Packet final static int ACK_SIZE = 8; //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 RBS final static int LOC_ACK_RBS_MSB = 6; //Location of LSB of RBS final static int LOC_ACK_RBS_LSB = 7; /** * @param args * @throws IOException * @throws InterruptedException */ public static void main(String[] args) throws IOException, InterruptedEx ception { // TODO Auto-generated method stub //newByte -> Random object Random newByte = new Random(); //timeoutCounter -> counts number of timeouts; dataSize -> Size

seqNumLsb -> MSB & LSB of the Sequence Number. lsbByte. sentArray = new byte[(int) rbs]. receivedChecksumLsb -> MSB & LSB of check sum of ACK packet //seqNumMsb. seqNumMsb.nextBytes(dataArray). msbB yte. //sentChecksum. msb Byte.out.println("\n"). //Generate array to be sent newByte. udpReceived. receivedArray = new byte[ACK_SIZE]. for(counter=0.getLocalHost().dataArray[counter]). . //sent and received UDP packets DatagramPacket udpSent. seqNum -> Sequence Number.length. //print sent bytes System. //timeout -> current timeout duration int counter.out."). sentChecksumLsb. //get server IP address serverIP = InetAddress. } System. //Declare clientSocket and initialize to NULL DatagramSocket clientSocket = null. dataSize = MPS-8.out. //Declare IP Addresss of the server InetAddress serverIP. //Until all bytes are sent while(seqNum<TOT_DAT) { //Timeout duration is initially 1s timeout = INIT_TIMEOUT. seqNum = INIT_SEQ_NUM. lsbByte -> MSB & LSB of RBS byte sentChecksumMsb. rbs -> Receive Buffer Size long sentChecksum. counter<dataArray.out. //dataArray -> Array of randomly generated data bytes //sentArray -> Array of bytes sent in current packet.println("THE SENT BYTES ARE:"). rbs = MPS. System. counter++){ if(counter%10 == 0) System. seqNumLsb.of current packet's payload.println("\n\n\nRUN CLIENT APPLICATION AGAIN TO TRANSM IT 500 MORE BYTES. pktType.out. //if the packet isn't the last to be sent if(dataSize<TOT_DAT-seqNum) //sentArray is of length RBS udpSent = new DatagramPacket(sentArray. timeoutCounter=0. try{ //TEST CODE SEGMENT//System. receivedAr ray -> Array of bytes in the ACK Packet byte[] dataArray = new byte[TOT_DAT].printf("\nRBSSSS SSSSSSSSSSSSSSSSSSSSSSSSSS %d\n". sentChecksumLsb -> MSB & LSB of checksum of s ent packet //receivedChecksumMsb. //sentChecksumMsb.rbs). receivedChecksum. //create client socket clientSocket = new DatagramSocket(). timeout = INIT_TIMEOUT. receivedChecksum -> Checksum of the sent and rec eived packets.printf("%x\t".

TOT_DAT-seqNum). (TOT_DAT-seqNum+8). SRV_PRT_NUM). } else { //length of the payload = remaining number of bytes to be sent sentArray[LOC_LEN_MSB] = (byte) (((TOT_DAT-seqNum)>>8)&0xff). seqNum. dataSize). //Length //if the packet isn't the last t o be sent if(dataSize<TOT_DAT-seqNum){ //length of the payload = dataSize sentArray[LOC_LEN_MSB] = (byte) ((dataSize>>8) & 0xff). //Increment Sequence Number seqNum += dataSize. SRV_PRT_NUM). sentArray[LOC_SEQ_NUM_LSB] = (byte) (seq Num & 0xff).seqNum). else //copy the remaining byt es to be sent System.dataSize).(int)rbs. else //Length of sentArray = length of the he ader + remaining number of data bytes to be sent udpSent = new DatagramPacket(sentArray. System. serverIP.*/ //Packet Type . 8. seqNum. sentArray[LOC_LEN_LSB] = (byte) ((TOT_DAT-seqNum) & 0xff). sentArray.out. sentArray.(byte)0). sentArray[LOC_LEN_LSB] = (byte) (dataSize & 0xff).printf("SeqNu m = %d\n". //build UDP packet //Data //Fill the Array to be sent with zeros just to ensure clean data Arrays. } //Sequence Number sentArray[LOC_SEQ_NUM_MSB] = (byte)((seq Num>>8) & 0xff). //TEST CODE SEGMENT /*System.fill(sentArray. 8.arraycopy(dataArr ay.out.arraycopy(dataArr ay. serverIP. //if the packet isn't the last t o be sent if(dataSize<TOT_DAT-seqNum) //copy the 'dataSize' nu mber of bytes System.printf("\nD ata Size = %d ".

//receive packet from server clientSocket. sentArray[LOC_PKT_TYP_LSB] = (by te) (pktType & 0xff). sentArray[LOC_CHECKSUM_LSB] = se ntChecksumLsb. else pktType = 0x0000.idx++) { if(idx%25==0) System.send(udpSent).out.println("\n****************").idx<receivedArray. sentChecksumMsb = (byte) ((sentC hecksum>>8) & 0xff).out. //calculate checksum of sent packet //Set Checksum field to zero sentArray[LOC_CHECKSUM_MSB] = 0.out. sentChecksumLsb.print f("\nSent Checksum MSB byte = %d. System.receivedArray[i dx]). sentArray[LOC_PKT_TYP_MSB] = (by te) ((pktType>>8) & 0xff).println().out. sentArray[LOC_CHECKSUM_LSB] = 0.setSoTimeout(timeout). sentChecksum = calculateChecksum (sentArray). received Array. } System.print f("\nSent Checksum LSB byte = %d. sentArray[LOC_CHECKSUM_MSB] = se ntChecksumMsb. reset timeoutCounter to zero and timeout duration to 1 second timeoutCounter = 0. //TEST CODE SEGMENT /* System.sentChecksum ). .length).out. sentChecksumMsb. sentChecksumMsb).println("\n******RECEIVED PKT****** ").out. //TEST CODE SEGMENT//System.printf("%x.if(seqNum>=TOT_DAT)//if last pac ket has been reached pktType = 0x0001. Hex: %x". sentChecksumLsb). */ //send UDP packet clientSocket. sentChecksumLsb =(byte) (sentChe cksum & 0xff). Hex: %x".length.out. printf("-----------------------------------------Calculation %x\n".receive(udpReceived). //Initialize received packet udpReceived = new DatagramPacket(receivedArray. ".*/ //On successfully sending a packet. //TEST CODE SEGMENT /*System. //set timer clientSocket. System. for(int idx =0.

seqNumLsb = (byte) seqNum. seqNumLsb).*/ //put msb & lsb together to form the sum rbs = ((msbByte<<8) & 0xff00) + (lsbByte & 0xff).out. .printf("----------------------------------------Verification %x\n". Hex: %x".out.*/ if((short)receivedChecksum==0x0 && receivedArray[LOC_PKT _TYP_MSB]==0x00 && receivedArray[LOC_PKT_TYP_LSB]==0x02 && receivedArray[LOC_ACK _NUM_MSB]==seqNumMsb && receivedArray[LOC_ACK_NUM_LSB]==seqNumLsb){ //set new RBS //calculate the msb & lsb in byte form msbByte = receivedArray[LOC_ACK_RBS_MSB] .rbs).receivedChecksum). seqNumMsb.printf("\nReceived Checksum MSB byte = %d. receivedChecksumMsb). lsbByte = receivedArray[LOC_ACK_RBS_LSB] . System.*/ //Get MSB and LSB of Sequence Number seqNumMsb = seqNum<0? (byte) ((seqNum/256)+255):(byte) ( seqNum/256). seqNumMsb). //The next payload will be (RBS-8) bytes long dataSize = (int) (rbs . receivedChecksumMsb. Hex: %x". rbsLsb = lsbByte>=0? lsbByte:lsbByte+256 . //calculate checksum of received packet receivedChecksum = calculateChecksum(receivedArray).printf("\nSequence Number LSB byte = %d. //TEST CODE SEGMENT /*//convert the byte forms of msb & lsb into int in order to remove the sign bit rbsMsb = msbByte>=0? msbByte:msbByte+256 . System. receivedChecksumMsb = receivedChecksum<0? (byte) ((receivedChecksum/256)+255):(byte) (receivedChecksum/256). receivedChecksumLsb = (byte) receivedChecksum.timeout=INIT_TIMEOUT.8). Hex: %x". //TEST CODE SEGMENT /*System. seqNumLsb.out. //TEST CODE SEGMENT//System. //increment timeoutCounter timeoutCounter++. receivedChecksumLsb. System.printf("\nReceived Checksum LSB byte = %d.out. Hex: %x". } }//try catch(Exception e){ //Until the 4th timeout if(timeoutCounter<=3){ //Keep doubling the timeout duration timeout*=2.printf("\nSequence Number MSB byte = %d. receivedChecksumLsb). //TEST CODE SEGMENT /*System.printf(" \n******RBS )))))))))))))))))))))))))))))))))))))))))******%x\n\n".out.out.

println("Client socket timeou t! Exception object e : "+e). checksum. msb.close(). //convert the byte forms of msb & lsb into int in order to remove the sign bit msb = msbByte>=0? msbByt e:msbByte+256. //sum -> unflipped checksum. lsbByte. counter++) { //calculate the msb & ls b in byte form msbByte = Array[2*counte r]. lsb = lsbByte>=0? lsbByt e:lsbByte+256. }//main private static long calculateChecksum(byte[] Array) { // TODO Auto-generated method stub //counter -> counter to calculate sum. counter<Array. msb. } }//catch }//while //Close the client socket if it's value is NULL if(clientSocket!=null) clientSocket. lsbByte = Array[2*counte r+1]. // msb & lsb in byte form byte msbByte.out.length%2==0) { for(counter = 0. long sum = 0. //calculate checksum //As long as the length of the array is even if(Array. } //if 4th timeout occurs else{ //Display the exception that occured System.l ength/2. lsb -> MSB & LSB wit h sign bit removed int counter.exit(0). lsb.//resend the packet with doubled timeout duratio n continue. //terminate the client application System. } //while the shifted sum results . //put msb & lsb together to form the sum sum += (msb<<8) + lsb.

length] = 0. thus making it even byte[] tempArray = new byte[Arra y. 0. System.arraycopy(Array.length).in a 1 (sign bit). //Recursive call to calculate ch ecksum of the zero-padded array. Original array is unaffected return calculateChecksum(tempArr ay). } //If the length of the array is odd else{ //Copy the array into a temporar y array and pad a zero. tempA rray. tempArray[Array. } }//calculateChecksum } . Array. return checksum. add sum with the shifted sum //this adds carries generated wh ile calculating 1s complement sum while(sum >> 16 != 0) sum = (sum >> 16) + (sum & 0xffff). //checksum is the 1s complement of the 1s complement sum checksum = ~sum. 0.length+1].