forked from icodeu/GBN_has_window
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GBNServer.java
46 lines (43 loc) · 1.76 KB
/
GBNServer.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
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.ArrayList;
public class GBNServer {
public static void main(String[] args) throws Exception {
DatagramSocket serverSocket = new DatagramSocket(8888);
int endReceive = -1;
while (true){
byte[] data = new byte[100];
DatagramPacket receivePacket = new DatagramPacket(data, data.length);
serverSocket.receive(receivePacket);
int seq = new String(receivePacket.getData()).charAt(0) - '0';
if (Math.random()<=0.7){
if (seq == endReceive + 1){
//接收成功,发ack;
byte[] ackData = new String("ack"+seq).getBytes();
InetAddress clientAddress = receivePacket.getAddress();
int clientPort = receivePacket.getPort();
DatagramPacket sendPacket = new DatagramPacket(ackData, ackData.length, clientAddress, clientPort);
serverSocket.send(sendPacket);
endReceive++;
System.out.println("服务端发送 ack= " + seq);
}else if (endReceive != -1){
byte[] ackData = new String("ack"+endReceive).getBytes();
InetAddress clientAddress = receivePacket.getAddress();
int clientPort = receivePacket.getPort();
DatagramPacket sendPacket = new DatagramPacket(ackData, ackData.length, clientAddress, clientPort);
serverSocket.send(sendPacket);
System.out.println("服务端发送 ack= " + endReceive);
}
// //接收成功,发ack;
// byte[] ackData = new String("ack"+hasReceive).getBytes();
// InetAddress clientAddress = receivePacket.getAddress();
// int clientPort = receivePacket.getPort();
// DatagramPacket sendPacket = new DatagramPacket(ackData, ackData.length, clientAddress, clientPort);
// serverSocket.send(sendPacket);
// if (seq == hasReceive)
// hasReceive++;
}
}
}
}