-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDGClient.java
More file actions
30 lines (27 loc) · 1.01 KB
/
Copy pathDGClient.java
File metadata and controls
30 lines (27 loc) · 1.01 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
import java.net.DatagramSocket;
import java.net.DatagramPacket;
import java.io.IOException;
import java.net.InetAddress;
public class DGClient{
public static void main(String[] args){
if(args.length!=1){
System.out.println("usage: java DGClient message");
return;
}
byte[] messageBytes = args[0].getBytes();
try{
byte[] buffer = new byte[128];
System.arraycopy(messageBytes, 0, buffer, 0, messageBytes.length);
DatagramSocket dgs = new DatagramSocket();
DatagramPacket dgp = new DatagramPacket(buffer, buffer.length, InetAddress.getByName(DGServer.SERVER_IP), DGServer.SERVER_PORT);
dgs.send(dgp);
// waiting and get echoing message
dgs.receive(dgp);
System.out.println("Client Receive Echo: " + new String(dgp.getData()));
// Explore the buffer size that DatagramSocket support by underlying platform.
System.out.println("ReceiveBufferSize: " + dgs.getReceiveBufferSize() + ", SendBufferSize: " + dgs.getSendBufferSize());
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}