发送和接收 UDP 数据包?
我制作了一个程序,将UDP数据包从客户端发送到服务器。
以下是发射机代码:
import java.io.IOException;
import java.net.*;
public class JavaApplication9 {
public static void main(String[] args) throws UnknownHostException, SocketException, IOException {
// TODO code application logic here
byte[] buffer = {10,23,12,31,43,32,24};
byte [] IP = {-64,-88,1,106};
InetAddress address = InetAddress.getByAddress(IP);
DatagramPacket packet = new DatagramPacket(
buffer, buffer.length, address, 57
);
DatagramSocket datagramSocket = new DatagramSocket();
datagramSocket.send(packet);
System.out.println(InetAddress.getLocalHost().getHostAddress());
}
}
接收器代码函数是这样的:
public void run() {
try {
DatagramSocket serverSocket = new DatagramSocket(port);
byte[] receiveData = new byte[8];
byte[] sendData = new byte[8];
while (true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String( receivePacket.getData());
System.out.println("RECEIVED: " + sentence);
InetAddress IPAddress = receivePacket.getAddress();
String sendString = "polo";
sendData = sendString.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
}
} catch (IOException e) {
e.printStackTrace();
}
}
我使用过Wireshark程序。UDP数据包在接收器的Wireshark程序中接收,但Java程序无法识别它,程序只是继续侦听端口而没有任何反应?