发送和接收 UDP 数据包?

2022-09-02 01:12:45

我制作了一个程序,将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程序无法识别它,程序只是继续侦听端口而没有任何反应?


答案 1

接收方必须将接收方的端口设置为与发送方 DatagramPacket 中设置的端口相匹配。对于调试,请尝试侦听端口 > 1024(例如 5000 或 9000)。端口 < 1024 通常由系统服务使用,需要管理员访问权限才能绑定到此类端口。

如果接收方将数据包发送到它正在侦听的硬编码端口(例如端口57),并且发送方位于同一台计算机上,那么您将创建一个到接收方本身的环回。始终使用从数据包中指定的端口,并且在生产软件的情况下需要检查以防止这种情况。

数据包无法到达目的地的另一个原因是发件人中指定的 IP 地址错误。与 TCP 不同,UDP 将尝试发送数据包,即使地址无法访问,发送方也不会收到错误指示。您可以通过在接收器中打印地址来检查这一点,作为调试的预防措施。

在您设置的发件人中:

 byte [] IP= { (byte)192, (byte)168, 1, 106 };
 InetAddress address = InetAddress.getByAddress(IP);

但以字符串形式使用地址可能更简单:

 InetAddress address = InetAddress.getByName("192.168.1.106");

换句话说,您将目标设置为 192.168.1.106。如果这不是接收方,则不会收到数据包。

这是一个简单的UDP接收器的工作原理:

import java.io.IOException;
import java.net.*;

public class Receiver {

    public static void main(String[] args) {
        int port = args.length == 0 ? 57 : Integer.parseInt(args[0]);
        new Receiver().run(port);
    }

    public void run(int port) {    
      try {
        DatagramSocket serverSocket = new DatagramSocket(port);
        byte[] receiveData = new byte[8];
        String sendString = "polo";
        byte[] sendData = sendString.getBytes("UTF-8");

        System.out.printf("Listening on udp:%s:%d%n",
                InetAddress.getLocalHost().getHostAddress(), port);     
        DatagramPacket receivePacket = new DatagramPacket(receiveData,
                           receiveData.length);

        while(true)
        {
              serverSocket.receive(receivePacket);
              String sentence = new String( receivePacket.getData(), 0,
                                 receivePacket.getLength() );
              System.out.println("RECEIVED: " + sentence);
              // now send acknowledgement packet back to sender     
              DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,
                   receivePacket.getAddress(), receivePacket.getPort());
              serverSocket.send(sendPacket);
        }
      } catch (IOException e) {
              System.out.println(e);
      }
      // should close serverSocket in finally block
    }
}

答案 2

推荐