Vous êtes sur la page 1sur 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

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

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

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

timeout=INIT_TIMEOUT; //calculate checksum of received packet receivedChecksum = calculateChecksum(receivedArray); //TEST CODE SEGMENT /*System.out.printf("----------------------------------------Verification %x\n",receivedChecksum); receivedChecksumMsb = receivedChecksum<0? (byte) ((receivedChecksum/256)+255):(byte) (receivedChecksum/256); receivedChecksumLsb = (byte) receivedChecksum; System.out.printf("\nReceived Checksum MSB byte = %d, Hex: %x", receivedChecksumMsb, receivedChecksumMsb); System.out.printf("\nReceived Checksum LSB byte = %d, Hex: %x", receivedChecksumLsb, receivedChecksumLsb);*/ //Get MSB and LSB of Sequence Number seqNumMsb = seqNum<0? (byte) ((seqNum/256)+255):(byte) ( seqNum/256); seqNumLsb = (byte) seqNum; //TEST CODE SEGMENT /*System.out.printf("\nSequence Number MSB byte = %d, Hex: %x", seqNumMsb, seqNumMsb); System.out.printf("\nSequence Number LSB byte = %d, Hex: %x", seqNumLsb, seqNumLsb);*/ 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] ; lsbByte = receivedArray[LOC_ACK_RBS_LSB] ; //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 ; rbsLsb = lsbByte>=0? lsbByte:lsbByte+256 ;*/ //put msb & lsb together to form the sum rbs = ((msbByte<<8) & 0xff00) + (lsbByte & 0xff); //TEST CODE SEGMENT//System.out.printf(" \n******RBS )))))))))))))))))))))))))))))))))))))))))******%x\n\n",rbs); //The next payload will be (RBS-8) bytes long dataSize = (int) (rbs - 8); } }//try catch(Exception e){ //Until the 4th timeout if(timeoutCounter<=3){ //Keep doubling the timeout duration timeout*=2; //increment timeoutCounter timeoutCounter++;

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

in a 1 (sign bit), 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; return checksum; } //If the length of the array is odd else{ //Copy the array into a temporar y array and pad a zero, thus making it even byte[] tempArray = new byte[Arra y.length+1]; System.arraycopy(Array, 0, tempA rray, 0, Array.length); tempArray[Array.length] = 0; //Recursive call to calculate ch ecksum of the zero-padded array; Original array is unaffected return calculateChecksum(tempArr ay); } }//calculateChecksum }

Vous aimerez peut-être aussi