-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUdpClient.java
29 lines (26 loc) · 1.02 KB
/
UdpClient.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
package task1;
import java.io.*;
import java.net.*;
public class UdpClient {
public static void main(String args[]) throws Exception {
// get server address and open socket
InetAddress serverAdr = InetAddress.getByName(args[0]);
DatagramSocket sock = new DatagramSocket();
// build packet addressed to server, then send it
byte[] outBuf = args[1].getBytes("US-ASCII");
DatagramPacket outPkt = new
DatagramPacket(outBuf, outBuf.length, serverAdr, 9876);
sock.send(outPkt);
System.out.println("Sent...");
// create buffer and packet for reply, then receive it
byte[] inBuf = new byte[1000];
DatagramPacket inPkt = new DatagramPacket(inBuf, inBuf.length);
sock.receive(inPkt);
System.out.println("Recevied...");
// print buffer contents and close socket
String reply = new String(inBuf, 0, inPkt.getLength(), "US-ASCII");
System.out.println(reply);
sock.close();
System.out.println("Socket closed...");
}
}