首页 > 解决方案 > 如何让 UDP 服务器响应 UDP 客户端数据包?

问题描述

我无法让服务器将数据发送回客户端。这是我的代码以及我的输入和结果。

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPServer {

    public static void main(String[] args) throws IOException {
        InetAddress ip = InetAddress.getByName("127.0.0.1");
        int port = 1345;

        //Creates connection socket.
        DatagramSocket serverSocket = new DatagramSocket(port);

        System.out.println("Server Active");
        while(true) {
            //Receiving packet
            byte[] buffer = new byte[100];
            DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length);
            serverSocket.receive(receivePacket);
            String clientInput = new String(buffer);
            System.out.println("Server has Received : " + clientInput);

            //Sending packet
            byte[] data = clientInput.getBytes();
            DatagramPacket sendPack = new DatagramPacket(data, data.length, ip, port);
            serverSocket.send(sendPack);
            String serverInput = new String(data);
            System.out.println("Server has sent: " + serverInput);
        }

    }
}

~~~~~~~~~~~~~~~~~

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramSocket;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;


public class UDPClient
{
    public static void main(String args[]) throws IOException
    {
        DatagramSocket clientSocket = new DatagramSocket();
        InetAddress ip = InetAddress.getByName("127.0.0.1");
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        int port = 1345;

        System.out.println("Would you like to continue?");
        while(inFromUser.readLine().equals("Yes") || inFromUser.readLine().equals("yes")) {
            System.out.println("Enter an expression to be evaluated");

            //gets userInput
            String userInput = inFromUser.readLine();
            byte[] buffer = userInput.getBytes();

            //sends packet to server
            DatagramPacket sendPacket = new DatagramPacket(buffer, buffer.length, ip, port);
            clientSocket.send(sendPacket);
            System.out.println("Client has sent: " + userInput);

            //receives packet from server
            byte[] data = new byte[100];
            DatagramPacket receivePacket = new DatagramPacket(data, data.length);
            clientSocket.receive(receivePacket);
            String serverInput = new String(data);
            System.out.println("Client has received: " + serverInput);
            System.out.println("Would you like to continue?");
        }
    }
}

我不确定为什么服务器不回显给客户端?我不确定我做错了什么,或者这段代码的整体质量。

标签: javasocketsioudp

解决方案


服务器正在向自己发送回复。当您sendPacket在服务器中创建时,您将提供它ipportipandport是服务器自己的 IP 和绑定(侦听)端口,而不是客户端的端口。

因为客户端创建它DatagramSocket时没有指定端口,系统会动态地为其选择一个未使用的端口号。您需要使用receivePacket.getSocketAddress()(在服务器中)获取客户端的端口号,然后在构建sendPacket. 事实上,有一个替代构造函数DatagramPacket可以接受缓冲区、长度,SocketAddress并且非常适合构造回复数据包。


推荐阅读