发送和使用Java收到UDP报文?(sending and receiving UDP packet

2019-06-24 08:47发布

我已经做了计划,从客户端发送一个UDP数据包到这里服务器发送源代码

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

/**
 *
 * @author hp
 */
public class JavaApplication9 {

    /**
     * @param args the command line arguments
     */
    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 (Exception e){
    }
}

我已经使用Wireshark的程序。 在UDP包被接收在Wireshark的程序在接收器,但Java程序就认不出来了,程序只是不断侦听的端口,并没有任何反应?

Answer 1:

接收器必须设置接收端口,以匹配发送DatagramPacket的端口设置。 为了调试尝试监听端口> 1024(例如8000或9000)。 端口<1024,通常使用的系统服务,需要管理员访问这些端口上绑定。

如果接收器发送数据包的硬编码的端口它的监听(如端口57)和发送者是同一台机器上,那么你将创建一个回环到接收器本身。 始终使用从包和生产软件的情况下指定的端口需要在任何情况下进行检查,以防止这种情况。

另一个原因是,数据包不会到达目的地在发件人指定了错误的IP地址。 UDP不像TCP将尝试发送一个数据包,即使地址不可达和发件人将不会收到一个错误指示。 您可以通过打印在接收器的地址作为用于调试的预防检查。

在发送端设置:

 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     
              InetAddress IPAddress = receivePacket.getAddress();
              DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,
                   IPAddress, receivePacket.getPort());
              serverSocket.send(sendPacket);
        }
      } catch (IOException e) {
              System.out.println(e);
      }
      // should close serverSocket in finally block
    }
}


文章来源: sending and receiving UDP packets using Java?