-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
96 lines (75 loc) · 2.54 KB
/
Server.java
File metadata and controls
96 lines (75 loc) · 2.54 KB
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
import java.net.InetAddress;
public class Server {
private MPSock mpSock;
public Server() {
}
// public int getPort() {
// return mpSock.getPort();
// }
// public InetAddress InetAddress() {
// return mpSock.getAddr();
// }
public void run(String[] args) {
Boolean running = true;
int numBytes = 0;
int targBytes;
if (args.length > 0) {
targBytes = Integer.parseInt(args[0]);
} else {
targBytes = 10000;
}
// int numPaths;
// if (args.length > 2) {
// numPaths = Integer.parseInt(args[1]);
// } else {
// numPaths = 1;
// }
int v;
if (args.length > 1) {
v = Integer.parseInt(args[1]);
} else {
v = 0;
}
try {
this.mpSock = new MPSock(InetAddress.getByName("127.0.0.1"), 4445, v);
} catch (Exception e) {
e.printStackTrace();
}
mpSock.bind(4445);
mpSock.listen(4);
mpSock.accept();
System.out.println("Server listening on port " + this.mpSock.getPort());
long startTime = System.currentTimeMillis();
while (running) {
try {
byte[] readBuf = new byte[500];
int readLen = mpSock.read(readBuf, 0, readBuf.length);
// System.out.println("readlen:" + readLen);
if (readLen > 0) {
for (int i = 0; i < readLen; i++) {
// System.out.print((int) readBuf[i]);
// System.out.print(",");
numBytes++;
}
System.out.println(numBytes);
}
if (numBytes >= targBytes){ // DO NOT CHANGE!
System.out.println(numBytes);
long elapse = System.currentTimeMillis() - startTime;
System.out.println("time elapsed:" + elapse + " | "
+ String.format("%.2f", (double) targBytes / (double) elapse * 1000 ) + " mbps" + " | flows:"
+ mpSock.getNumConnections());
running = false;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
mpSock.close();
}
public static void main(String[] args) {
Server server = new Server();
server.run(args);
}
}