-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUdpClient.java
More file actions
27 lines (25 loc) · 918 Bytes
/
UdpClient.java
File metadata and controls
27 lines (25 loc) · 918 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
package com.socket;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* UDP客户端。
*/
public class UdpClient {
public static void main(String[] args){
try {
DatagramSocket client = new DatagramSocket(5070);
DatagramPacket packet = new DatagramPacket(new byte[1024],1024);
packet.setPort(5060);
packet.setAddress(InetAddress.getLocalHost());
packet.setData("HelloWorld Server".getBytes());
client.send(packet);
client.receive(packet);
System.out.println(packet.getAddress().getHostName() + "(" + packet.getPort() + "):" + new String(packet.getData()));
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}