import java.io.*; import java.net.*; import java.util.*; class general{ public static byte[] rdtPacket= new byte[100]; // The structure of rdt packet is Ver-Type-Seq/Ack Number - data // for example for data =00101010 and type 0 and seqnumber 5 // rdtPacket = 0010010100101010 ---> 001(Ver) 00(type) 101 (seqNumber) 00101010 (data) // The rdt packet fields : public static int type ; public static int seqNumber; public static byte[] data = new byte[99]; // the following function creates rdtPacket. It computes the value of the header base on // the value of type and seq Number and version. It attaches the data at the end of the packet. public static void makeRDTPacket ( ){ byte header = (byte)0; // create Version header = 1 << 5; // shift left type such that it is moved to its correct position in the header and convert the result to byte, type = (byte) type << 3; byte typeField = (byte)type; //convert seqNumber to byte byte seqField = (byte) seqNumber; // combine version, type and seqNumber and create the header header = (byte)(header | typeField | seqField ); rdtPacket[0] = header; // add data at the end of rdt packet. for (int i=0; i>3; Byte seqNumberField = (byte) (header.byteValue() & (byte) 7); //AND with 00000111 seqNumber = seqNumberField.intValue(); //convert the result to int for (int i =1; i < rdtPacket.length ; i++) //get data data[i-1] = rdtPacket[i]; } }