如何让UDP服务器响应UDP客户端数据包?

时间:2018-04-26 20:00:28

标签: java sockets io 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?");
        }
    }
}

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

1 个答案:

答案 0 :(得分:4)

服务器正在向自己发送回复。当您在服务器中创建sendPacket时,您将为其提供ipport,但ipport是服务器自己的IP并绑定(监听)端口,而不是客户端口。

由于客户端在未指定端口的情况下创建了DatagramSocket,因此系统将为其动态选择未使用的端口号。您需要使用receivePacket.getSocketAddress()(在服务器中)获取客户端的端口号,然后在构造sendPacket时使用该地址。实际上,DatagramPacket有一个替代构造函数,它接受缓冲区,长度和SocketAddress,这是构造回复数据包的理想选择。