-
Notifications
You must be signed in to change notification settings - Fork 0
/
UDPThroughputServer.java
86 lines (71 loc) · 2.97 KB
/
UDPThroughputServer.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
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
public class UDPThroughputServer {
// Class constants
static final int PORT = 10720; // Port number assigned in class
private static final int ITERATIONS[] = {1024, 2048, 8192}; // Array containing number of iterations for calculations
private static final int BYTESIZES[] = {1024, 512, 128}; // Array containing byte sizes of sent messages
private static final int ACK_SIZE = 8; // Size of acknowledgement in the reverse direction
private static final long ACK = 98765432123456789L; // 8-byte acknowledgement in the reverse direction
// Initialize InetSocketAddresses, DatagramChannel, and ByteBuffers
private static InetSocketAddress address = null;
private static InetSocketAddress clientAddress = null;
private static DatagramChannel channel = null;
private static ByteBuffer buffer = null;
private static ByteBuffer ackBuffer = null;
//*********************************************************************************************
//
// Main
//
public static void main(String[] args) {
try {
// Open a datagramChannel and bind it to address
channel = DatagramChannel.open();
address = new InetSocketAddress(PORT);
channel.bind(address);
// Display on server
System.out.println("Server started on port #" + PORT);
System.out.println("Waiting for a client...");
// Measure TCP throughput averages
measureUDPThroughputAverage(BYTESIZES[0], ITERATIONS[0]);
measureUDPThroughputAverage(BYTESIZES[1], ITERATIONS[1]);
measureUDPThroughputAverage(BYTESIZES[2], ITERATIONS[2]);
System.out.println("UDP throughput measured...");
// Close the connection
channel.close();
System.out.println("Channel closed.");
} catch(IOException i) {
System.out.println(i);
}
}
//*********************************************************************************************
//
// Measure UDP Throughput Average
//
private static void measureUDPThroughputAverage(int byteSize, int iterations)
{
// Start server and wait for a connection
try {
// Allocate byte buffer for byteSize bytes
buffer = ByteBuffer.allocate(byteSize);
// Allocate byte buffer for ACK_SIZE bytes
ackBuffer = ByteBuffer.allocate(ACK_SIZE);
// Put ACK into ackBuffer
ackBuffer.putLong(ACK);
// Get average from ITERATIONS number of round trips
for (int i = 0; i < iterations; i++) {
// Clear buffer of any previous data
buffer.clear();
// Receive data from the client
clientAddress = (InetSocketAddress) channel.receive(buffer);
// Send ACK echo back to client
ackBuffer.flip();
channel.send(ackBuffer, clientAddress);
}
} catch(IOException i) {
System.out.println(i);
}
}
}