0

我正在尝试了解 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 程序中为服务器所做的那样,以便可以将消息返回到特定端口上的客户端。

例如,如果客户端将向服务器发送 UDP 消息,则服务器将在端口号 9876 上启动并运行,并在端口 9877 上将原始消息返回给客户端。请帮助。

4

1 回答 1

1

您不需要客户端的固定端口,就像客户端需要固定的 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接收和发送都使用相同的方法,然后只更改数据。

于 2016-04-04T09:55:04.647 回答