-
Notifications
You must be signed in to change notification settings - Fork 0
/
gobackn.java
190 lines (164 loc) · 4.4 KB
/
gobackn.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import java.io.*;
import java.util.*;
import java.net.*;
import java.lang.Math;
/*
The sender sends a packet from arr which is a number from the range 0 - 2^k - 1
- There's 3 pointers, start, end, pointer. Ignore end, its useless
- start points to the start of the sliding window,
- pointer points to the current position in the window
- It sends all packets inside the window one by one in each iteration without checking for ack
- Then it waits for a response from the client/receiver
- If the response is -1, theres an error and it does nothing and moves on
- Otherwise it checks if arr[start] == received message
- In that case, the packet has been acknowledged and the window can slide forward, and wait for the next ack
*/
class Sender{
Socket socket = null;
ObjectInputStream inp = null;
ObjectOutputStream out = null;
Sender(String address, int port){
try{
socket = new Socket(address, port);
System.out.println("Connected!");
out = new ObjectOutputStream(socket.getOutputStream());
out.flush();
inp = new ObjectInputStream(socket.getInputStream());
System.out.println("HELLO");
}
catch(Exception e){
System.out.println("Server: Oh noes");
}
}
public void run(int k, int n){
//Packets
int arr [] = new int[n];
//Window size
int mod = (int) Math.pow(2, k);
for(int i = 0 ; i < n ; i ++){
arr[i] = i%mod;
}
int start = 0, end = Math.min(n, mod - 2), pointer = 0;
do{
try{
// Send a packet
System.out.println("Sending " + arr[start + pointer] + " :" + start);
out.writeObject(arr[start + pointer]);
pointer = (pointer + 1)%mod;
// Read ack
int message = (int) inp.readObject();
if(message == -1){
// for errors, or when unexpected packet sent
}
else if(message == arr[start]){
// Increment window if correct ACK was received
System.out.println("ACK" + message + " received");
start ++;
end++;
pointer = 0;
}
}
catch(Exception e){
break;
}
} while(start < n);
try{
out.writeObject(-1);
} catch(Exception e){}
while(true){
try{
socket.close();
inp.close();
out.close();
break;
}
catch(Exception e){
}
}
}
}
/*
Client has one pointer -> counter. This is the sliding window of size 1
- It starts by reading a message
- If the message is -1, it stops(this is how i end the sequence)
- Otherwise it checks if received message == counter
- If its not, it gives -1, otherwise an ack
*/
class Client{
Socket socket = null;
ObjectInputStream inp = null;
ObjectOutputStream out = null;
Client(int port){
try{
ServerSocket serversocket = new ServerSocket(port);
serversocket.setSoTimeout(10000000);
System.out.println("Client is accepting connections");
Socket socket = serversocket.accept();
System.out.println("Connected!");
out = new ObjectOutputStream(socket.getOutputStream());
out.flush();
inp = new ObjectInputStream(socket.getInputStream());
}
catch(Exception e){
System.out.println("Client: Oh noes");
}
}
public void run(int k){
int dropped = 0;
int counter = 0;
int mod = (int) Math.pow(2, k);
System.out.println("Running");
do{
try{
// Get a packet
int message = (int) inp.readObject();
// End of packets - stop
if(message == -1) break;
// Randomly drop at most 3 packets over time
if(dropped < 3 && (int) (Math.random()*10) > 7){
System.out.println("Dropped " + message);
dropped += 1;
out.writeObject(-1);
continue;
}
// Window size is 1, if the correct one is received
// Increment the window and send ACK
if(message == counter){
System.out.println("Received " + message);
out.writeObject(message);
counter = (counter+1)%mod;
}
else{
out.writeObject(-1);
}
} catch(Exception e){
}
} while(true);
try{
socket.close();
inp.close();
out.close();
} catch(Exception e){}
}
}
class gobackn{
public static void main(String args []){
Scanner sc = new Scanner(System.in);
System.out.println("Enter sequence number size (bits)");
int k = sc.nextInt();
System.out.println("Enter number of packets");
int n = sc.nextInt();
System.out.println("1. Client\n2.Server");
int choice = sc.nextInt();
int port = 5000;
String address = "localhost";
if(choice == 2){
Sender sender = new Sender(address, port);
sender.run(k, n);
}
else{
Client client = new Client(port);
client.run(k);
}
}
}