如何 运行 UDP 无连接客户端-服务器对中固定端口上的客户端程序 java

How to run Client program on fixed port in UDP connectionless client - server pair in java

我正在尝试了解 UDP 无连接客户端-服务器对。我在《计算机网络:一种自上而下的方法》一书中得到了一些代码。 节目如下:- UDPServer.java:

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

    class UDPServer
   {
       public static void main(String args[]) throws Exception
       {
        DatagramSocket serverSocket = new DatagramSocket(9876);
        byte[] receiveData = new byte[1024];
        byte[] sendData = new byte[1024];
        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();
          int port = receivePacket.getPort();
          String capitalizedSentence = sentence.toUpperCase();
          sendData = capitalizedSentence.getBytes();
          DatagramPacket sendPacket =
          new DatagramPacket(sendData, sendData.length, IPAddress, port);                   
          serverSocket.send(sendPacket);
          }
      }
    }

UDPClient.java

import java.io.*;
import java.net.*;
class UDPClient
{
    public static void main(String args[]) throws Exception
    {
        BufferedReader inFromUser =
        new BufferedReader(new InputStreamReader(System.in));
        DatagramSocket clientSocket = new DatagramSocket();
        InetAddress IPAddress = InetAddress.getByName("localhost");
        byte[] sendData = new byte[1024];
        byte[] receiveData = new byte[1024];
        String sentence = inFromUser.readLine();
        sendData = sentence.getBytes();
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);      
        clientSocket.send(sendPacket);
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);       
        clientSocket.receive(receivePacket);
        String modifiedSentence = new String(receivePacket.getData());
        System.out.println("FROM SERVER:" + modifiedSentence);          
        clientSocket.close();
    }
}

在给定的代码中,我们已经为服务器固定了端口号,即 9876。 我很想知道如何修复客户端的端口,就像我们在给定的 java 程序中为服务器所做的那样,以便该消息可以 returned 到特定端口上的客户端。

例如,如果 客户端将向服务器发送 UDP 消息,服务器将启动并 运行 端口号 9876 和 return 原始消息到端口 9877 上的客户端。请帮助。

您不需要客户端的固定端口,就像客户端不需要固定的 IP 地址一样。您自己的代码应该已经可以正常工作了。但是还有其他问题:

String sentence = new String( receivePacket.getData());

错了。应该是 String sentence = new String( receivePacket.getData(), receivePacket.getOffset(), receivePacket.getLength());.

      System.out.println("RECEIVED: " + sentence);
      InetAddress IPAddress = receivePacket.getAddress();
      int port = receivePacket.getPort();
      String capitalizedSentence = sentence.toUpperCase();
      sendData = capitalizedSentence.getBytes();
      DatagramPacket sendPacket =
      new DatagramPacket(sendData, sendData.length, IPAddress, port);                   
      serverSocket.send(sendPacket);

此代码会将回复发送回请求发出的地方。更简单的方法是对接收和发送使用相同的 DatagramPacket,只需更改数据即可。