Skip to content

Commit c7552a5

Browse files
committed
Program files
1 parent c4afd11 commit c7552a5

File tree

6 files changed

+398
-0
lines changed

6 files changed

+398
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

Binaries/Packet.class

2.15 KB
Binary file not shown.

Binaries/RX.class

3.23 KB
Binary file not shown.

Binaries/Sender.class

3.65 KB
Binary file not shown.

RX.java

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
import java.io.IOException;
2+
import java.util.ArrayList;
3+
import java.util.Collections;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
import java.io.File;
7+
import java.io.FileOutputStream;
8+
import java.io.OutputStream;
9+
import java.io.InputStream;
10+
import java.io.PrintStream;
11+
import java.net.Socket;
12+
import java.net.InetSocketAddress;
13+
14+
public class RX {
15+
private Socket socket = null;
16+
private InputStream inputStream = null;
17+
private List<Packet> pList = null;
18+
19+
public RX(String host, int port) throws IOException {
20+
socket = new Socket();
21+
socket.connect( new InetSocketAddress(host,port) );
22+
23+
// ADD print here if you want
24+
System.out.println("connected to "+socket.getInetAddress().getHostAddress()+" on port "+port);
25+
26+
}
27+
28+
public void close() throws IOException {
29+
30+
// ADD your code here
31+
socket.close();
32+
}
33+
34+
public int run(String fname) {
35+
try {
36+
get_pkts();
37+
order_pkts();
38+
write_pkts(fname);
39+
close();
40+
} catch (Exception e) { e.printStackTrace(); }
41+
42+
return 0;
43+
}
44+
45+
/* get_pkts: pull all packets off the wire and store in pList
46+
this method also prints the packet info stats */
47+
public int get_pkts ( ) throws IOException {
48+
49+
// ADD your variables here
50+
byte[] recvPacket = new byte[1506];
51+
pList = new ArrayList<Packet>();
52+
long delay, totaldelay = 0, delayChecker = System.currentTimeMillis();
53+
int packetSizeRead = 0, totalSize = 0;
54+
55+
int npackets = 0; // how many packets read
56+
57+
/* loop: get all packets and capture stats
58+
must use getInputStream.read() */
59+
60+
while((packetSizeRead = socket.getInputStream().read(recvPacket, 0, 1506)) != -1) {
61+
delay = System.currentTimeMillis() - delayChecker;
62+
delayChecker = System.currentTimeMillis();
63+
totaldelay += delay;
64+
totalSize = totalSize + (packetSizeRead-6);
65+
System.out.print("Pkt "+(++npackets)+" ");
66+
Packet rPacket = new Packet(recvPacket);
67+
System.out.println("delay = "+delay+" ms");
68+
if (!pList.contains(rPacket)){
69+
pList.add(rPacket);
70+
}
71+
} // while (read from socket)
72+
73+
// ADD print here
74+
System.out.println("Total "+npackets+" packets / "+totalSize+" bytes recd. Total delay = "+totaldelay+" ms, average = "+(totaldelay/npackets)+ " ms");
75+
76+
return npackets;
77+
}
78+
79+
public void write_pkts(String f) throws Exception {
80+
// this must call Packet.write() for each Packet
81+
FileOutputStream fos = new FileOutputStream(f);
82+
for (int i = 0; i < pList.size(); i++){
83+
pList.get(i).write(fos);
84+
}
85+
fos.close();
86+
}
87+
88+
// put the pList in the correct order
89+
// and remove duplicates
90+
public void order_pkts() {
91+
Collections.sort(pList);
92+
}
93+
94+
public static void main(String[] args) {
95+
96+
if(args.length != 3) {
97+
System.out.println("Usage: host port filename");
98+
return;
99+
}
100+
101+
try {
102+
RX recv = new RX( args[0],
103+
Integer.parseInt(args[1]));
104+
recv.run ( args[2] );
105+
} catch (Exception e) { e.printStackTrace(); }
106+
} // main()
107+
108+
} // class RX
109+
110+
/* Packet class */
111+
class Packet implements Comparable<Packet>{
112+
/* DO_NOT change these private fields */
113+
private byte[] payload;
114+
private int seqNo;
115+
private short len;
116+
117+
public Packet(byte[] buf) {
118+
seqNo = get_seqno(buf); // must use only this method to get sequence no
119+
len = get_len(buf); // must use only this method to get length
120+
121+
// ADD code here
122+
System.out.print("SEQ=" + seqNo + ", len=" + len + ", ");
123+
124+
payload = new byte[len];
125+
int arrayIter = 0;
126+
for(int loopIter = 6; loopIter < len+6; loopIter++){
127+
payload[arrayIter++] = buf[loopIter];
128+
}
129+
} // Packet CTOR
130+
131+
/*Retrieve the sequence number from the packet header. The function converts the
132+
* stream bytes to bits and converts the bits to an integer. */
133+
private int get_seqno(byte []b) {
134+
int seqno = 0;
135+
136+
int[] bits = new int[32];
137+
int temp = 0;
138+
for(int j=0; j<4; j++){
139+
temp = (int) b[j];
140+
if(temp < 0){
141+
temp = (int) b[j] + 256;
142+
}
143+
for(int k = ((j+1)*8)-1; k >= (j*8); k--){
144+
bits[k] = temp%2;
145+
temp /= 2;
146+
}
147+
}
148+
149+
int powerValue = 1;
150+
for(int l = bits.length-1; l >= 1; l--){
151+
if(bits[l] == 1){
152+
seqno += powerValue;
153+
}
154+
powerValue = powerValue*2;
155+
}
156+
157+
return seqno;
158+
}
159+
160+
/*Retrieve the length of the payload from the packet header. The function converts the
161+
* stream bytes to bits and converts the bits to an integer.*/
162+
private short get_len(byte []b) {
163+
short len = 0;
164+
165+
int[] bits = new int[16];
166+
int temp = 0;
167+
for(int j=4; j<6; j++){
168+
temp = (int) b[j];
169+
if(temp < 0){
170+
temp = (int) b[j] + 256;
171+
}
172+
for(int k = ((j+1-4)*8)-1; k >= ((j-4)*8); k--){
173+
bits[k] = temp%2;
174+
temp /= 2;
175+
}
176+
}
177+
178+
int powerValue = 1;
179+
for(int l = bits.length-1; l >= 0; l--){
180+
if(bits[l] == 1){
181+
len += powerValue;
182+
}
183+
powerValue = powerValue*2;
184+
}
185+
186+
return len;
187+
}
188+
189+
// write this Packet to file: no need to change this
190+
public void write(FileOutputStream f) throws IOException {
191+
f.write(payload);
192+
}
193+
194+
@Override
195+
public int compareTo(Packet P1){
196+
return this.seqNo - P1.seqNo;
197+
}
198+
199+
@Override
200+
public boolean equals(Object P1){
201+
if(this.seqNo == ((Packet)P1).seqNo){
202+
return true;
203+
}
204+
return false;
205+
}
206+
} // class Packet

Sender.java

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
import java.io.FileInputStream;
2+
import java.io.FileNotFoundException;
3+
import java.net.Socket;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.Random;
7+
import java.net.ServerSocket;
8+
9+
/* Running the sender
10+
java Sender 2002 sample.txt 1 1
11+
java classFile port file testOption randomPayload
12+
13+
testOption range 1-5
14+
randomPayload - give any value to enable
15+
16+
testOption
17+
1 for duplicate packets
18+
2 for outOFOrder packets
19+
3 for droping packets
20+
4 for corrupt packets
21+
5 enables all
22+
Any other value default send
23+
24+
Implemented duplicate packets and outOfOrder packets
25+
*/
26+
27+
public class Sender {
28+
public static void main(String args[]) {
29+
30+
boolean duplicates = false, outOfOrder = false, dropPackets = false, corruptPackets = false;
31+
boolean randomGen = false;
32+
int dupTimes = 10, oooTimes = 14, dropTimes = 15, corTimes = 20, pktNo = 1;
33+
int returnTimes = oooTimes * 2 - 4;
34+
int dupPacketSent = 0, oooPacSent = 0;
35+
36+
//Choosing the testing parameters for sending.
37+
if (args.length > 2) {
38+
switch (Integer.parseInt(args[2])) {
39+
case 1:
40+
duplicates = true;
41+
break;
42+
case 2:
43+
outOfOrder = true;
44+
break;
45+
case 3:
46+
//Drop packets not implemented
47+
dropPackets = true;
48+
break;
49+
case 4:
50+
//Corrupt packets not implemented
51+
corruptPackets = true;
52+
break;
53+
case 5:
54+
duplicates = true;
55+
outOfOrder = true;
56+
dropPackets = true;
57+
corruptPackets = true;
58+
break;
59+
default:
60+
duplicates = false;
61+
outOfOrder = false;
62+
dropPackets = false;
63+
corruptPackets = false;
64+
}
65+
}
66+
if (args.length > 3) {
67+
randomGen = true;
68+
}
69+
70+
ArrayList<byte[]> oooList = new ArrayList<byte[]>();
71+
72+
try {
73+
//Socket creation
74+
ServerSocket serverSocket = new ServerSocket(Integer.parseInt(args[0]));
75+
Socket socket = serverSocket.accept();
76+
System.out.println("Client connected");
77+
FileInputStream fis = new FileInputStream(args[1]);
78+
79+
byte[] fileContents = new byte[1506];
80+
Random rand = new Random();
81+
int noOfBytesRead = 0, arraySize = 1500;
82+
long timer = System.currentTimeMillis(), timeChecker = 0;
83+
84+
if (randomGen) {
85+
//Not required for fixed packet size, like a tcp packet which would ideally have 1500 bytes
86+
arraySize = rand.nextInt(1501);
87+
}
88+
noOfBytesRead = fis.read(fileContents, 6, arraySize);
89+
90+
//Initiate sequence number
91+
int seqNo = 5;
92+
int[] bits = new int[48];
93+
94+
//Execute until end of the file is reached
95+
while (noOfBytesRead != -1) {
96+
97+
//arraySize is the no of bytes that is sent a payload
98+
if (noOfBytesRead < arraySize) {
99+
arraySize = noOfBytesRead;
100+
}
101+
102+
//Initiating a packet with header size 6
103+
byte[] packet = new byte[6 + arraySize];
104+
System.out.print("SeqNo: " + seqNo);
105+
int tempSeq = seqNo;
106+
//First bit is not used.
107+
bits[0] = 0;
108+
109+
//4 bytes - 1 bit of sequence numbers
110+
for (int j = 31; j >= 1; j--) {
111+
bits[j] = tempSeq % 2;
112+
tempSeq /= 2;
113+
}
114+
System.out.print(", PayloadSize: " + arraySize + ", PacketSize: " + (arraySize + 6));
115+
int temparraySize = arraySize;
116+
117+
//2 bytes of payload size
118+
for (int j = 47; j >= 32; j--) {
119+
bits[j] = temparraySize % 2;
120+
temparraySize /= 2;
121+
}
122+
int processBits = 0, fileArrayIter = 0;
123+
do {
124+
int byteValue = 0;
125+
int powerValue = 128;
126+
127+
// Converting a byte into its corresponding decimal value and assigning to the byte array
128+
for (int j = 0; j < 8; j++) {
129+
if (powerValue == 0)
130+
powerValue = 1;
131+
byteValue += bits[processBits] * powerValue;
132+
powerValue /= 2;
133+
processBits++;
134+
}
135+
if (byteValue > 127)
136+
byteValue -= 256;
137+
fileContents[fileArrayIter++] = (byte) byteValue;
138+
} while (processBits < 48);
139+
packet = Arrays.copyOfRange(fileContents, 0, 6 + arraySize);
140+
141+
//Storing packets to be sent later
142+
if (outOfOrder && pktNo % oooTimes == 0) {
143+
System.out.println(".\n\t Storing the packet with seq No " + (seqNo) + " packet for sending later");
144+
oooList.add(packet);
145+
oooPacSent++;
146+
} else {
147+
//Sending packets
148+
System.out.println(", Packet delay is 20 ms");
149+
Thread.sleep(20);
150+
socket.getOutputStream().write(fileContents, 0, arraySize + 6);
151+
if (duplicates && pktNo % dupTimes == 0) {
152+
int duplicatesCount = rand.nextInt(3);
153+
154+
//Sending duplicate packets
155+
while (duplicatesCount-- > -1) {
156+
dupPacketSent++;
157+
System.out.println("Sending a duplicate packet");
158+
Thread.sleep(20);
159+
socket.getOutputStream().write(packet, 0, arraySize + 6);
160+
}
161+
}
162+
}
163+
seqNo += arraySize;
164+
if (randomGen) {
165+
arraySize = rand.nextInt(1501);
166+
}
167+
noOfBytesRead = fis.read(fileContents, 6, arraySize);
168+
169+
//Send the stored packets at equal intervals or when the entire file has been sent
170+
if (outOfOrder && ((pktNo % returnTimes == 0 && !oooList.isEmpty()) || (noOfBytesRead == -1))) {
171+
while (!oooList.isEmpty()) {
172+
System.out.println("Sending a out of order packet");
173+
Thread.sleep(20);
174+
socket.getOutputStream().write(oooList.get(0), 0, oooList.get(0).length);
175+
oooList.remove(0);
176+
}
177+
}
178+
pktNo++;
179+
}
180+
timeChecker = System.currentTimeMillis();
181+
System.out.println("Packet sent correctly is " + (pktNo - 1 - oooPacSent));
182+
System.out.println("Packet sent outOfOrder is " + oooPacSent);
183+
System.out.println("Packet sent as duplicates is " + dupPacketSent);
184+
System.out.println("Total time taken for sender to send: " + ((timeChecker - timer) / 1000) + " seconds");
185+
fis.close();
186+
socket.close();
187+
serverSocket.close();
188+
} catch (Exception e) {
189+
System.out.println(e.getMessage());
190+
}
191+
}
192+
}

0 commit comments

Comments
 (0)