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