看了下UDP編程的書
Java代碼
import java
import
import
import
import
public class UDPReceive {
public static void main(String[] args) {
Integer port =
byte[] buffer = new byte[
try {
DatagramSocket datagramSocket = new DatagramSocket(port);
DatagramPacket datagramPacket = new DatagramPacket(buffer
buffer
try {
for (;;) {
System
datagramSocket
System
datagramPacket
}
} catch (IOException e) {
e
}
} catch (SocketException e) {
e
}
}
}
這個類用來接受消息
Java代碼
import java
import
import
import
import
import
public class UDPSend {
public static void main(String[] args) {
try {
for (;;) {
DatagramSocket sendSocket = new DatagramSocket();
DatagramPacket dataPack = new DatagramPacket(
try {
sendSocket
sendSocket
} catch (IOException e) {
e
}
}
} catch (SocketException e) {
e
} catch (UnknownHostException e) {
e
}
}
}
這個類用來發送消息
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26024.html