-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDGServer.java
More file actions
30 lines (25 loc) · 904 Bytes
/
Copy pathDGServer.java
File metadata and controls
30 lines (25 loc) · 904 Bytes
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
import java.net.DatagramSocket;
import java.net.DatagramPacket;
import java.io.IOException;
public class DGServer{
public static final int SERVER_PORT = 10000;
public static final String SERVER_IP = "localhost";
public static void main(String[] args){
System.out.println("Server started...");
try{
DatagramSocket dgs = new DatagramSocket(SERVER_PORT);
// Explore the buffer size that DatagramSocket support by underlying platform.
System.out.println("ReceiveBufferSize: " + dgs.getReceiveBufferSize() + ", SendBufferSize: " + dgs.getSendBufferSize());
byte[] buffer = new byte[128];
DatagramPacket dgp = new DatagramPacket(buffer, buffer.length);
while(true){
dgs.receive(dgp);
System.out.println("Receiving: " + new String(dgp.getData()));
// echoing back the data
dgs.send(dgp);
}
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}