Java上的UDP客户端-服务器均在本地计算机上,客户端程序未响应

时间:2020-09-03 07:55:18

标签: java sockets udp client-server

我正在使用UDP协议在Java中模拟基本的客户端-服务器聊天应用程序,问题是我的客户端程序没有响应服务器程序发送的消息,首先,服务器向服务器发送了一条初始化消息,客户端应该从各自的PowerShell终端读取一个字符串,然后将该消息发送到服务器程序。在调试代码时,我意识到服务器程序能够发送启动消息,但是客户端程序未从中获取输入它的终端,您能看到吗,原谅我,如果听起来有点愚蠢,但我是计算机网络课程的初学者。 (客户端和服务器均在我的本地计算机上运行)

服务器程序

import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.charset.*;



public class ServerUDP {
    private static int SERVERPORT;
    private static int CLIENTPORT;
    private static InetAddress SERVERADDRESS;
    public DatagramSocket ServerSocket;
    public BufferedReader br;
    private int MSGLEN;
    
    void ConnectionSetup() throws Exception{
        SERVERPORT = 3001;
        CLIENTPORT = 3000;
        MSGLEN = 100;
        SERVERADDRESS = InetAddress.getLocalHost();
        br = new BufferedReader(new InputStreamReader(System.in));
        ServerSocket = new DatagramSocket(SERVERPORT);
    }

    void ConnectionClose() throws Exception{
        br.close();
        ServerSocket.close();
    }

    void SendMessage(String mString) throws Exception{
        byte[] BuffToClient = new byte[this.MSGLEN];
        BuffToClient = mString.getBytes();
        DatagramPacket PktToClient = new DatagramPacket(BuffToClient, BuffToClient.length,SERVERADDRESS,CLIENTPORT);
        ServerSocket.send(PktToClient);
    }

    String GetMessage() throws Exception{
        byte[] BuffFromClient = new byte[this.MSGLEN];
        DatagramPacket PktFromClient = new DatagramPacket(BuffFromClient,BuffFromClient.length);
        ServerSocket.receive(PktFromClient);
        BuffFromClient = PktFromClient.getData();
        return new String(BuffFromClient,StandardCharsets.UTF_8);
    }

    void Converse() throws Exception{
        this.SendMessage("Hello Client, How are you?");
        System.out.println("msg sent");
        while(true){
            String CliMsg = this.GetMessage();
            if(CliMsg.equals("bye")){
                this.SendMessage("Ok bye");
                break;
            }else{
                System.out.println("Client Says : "+CliMsg);
                this.SendMessage(br.readLine());
            }
        }
    }

    public static void main(String[] args) throws Exception{
        ServerUDP Server = new ServerUDP();
        Server.ConnectionSetup();
        Server.Converse();
        Server.ConnectionClose();
    }    
}

客户计划

import java.io.*;
import java.net.InetAddress;
import java.nio.charset.*;
import java.net.*;


public class ClientUDP {
    public static int CLIENTPORT;
    public static int SERVERPORT;
    public InetAddress CLIENTADD;
    public InetAddress SERVERADDRESS;
    public int MSGLEN;
    public static BufferedReader br;
    public DatagramSocket clientSocket;

    void ConnectionSetup() throws Exception{
        CLIENTPORT = 3000;
        SERVERPORT = 3001;
        MSGLEN = 100;
        CLIENTADD = InetAddress.getLocalHost();
        SERVERADDRESS = InetAddress.getLocalHost();
        br = new BufferedReader(new InputStreamReader(System.in));
        clientSocket = new DatagramSocket(CLIENTPORT);
    }

    void ConnectionClose() throws Exception{
        br.close();
        clientSocket.close();
    }

    void SendMessage(String mString) throws Exception{
        byte[] BuffToServer = new byte[this.MSGLEN];
        BuffToServer = mString.getBytes();
        DatagramPacket PktToServer = new DatagramPacket(BuffToServer,BuffToServer.length,CLIENTADD,SERVERPORT);
        clientSocket.send(PktToServer);
    }

    String GetMessage() throws Exception{
        byte[] BuffFromServer = new byte[this.MSGLEN];
        DatagramPacket PktFromServer = new DatagramPacket(BuffFromServer, BuffFromServer.length);
        clientSocket.receive(PktFromServer);
        BuffFromServer = PktFromServer.getData();
        System.out.println("msg recived");
        for(byte b : BuffFromServer){
            System.out.println(b);
        }
        return new String(BuffFromServer,StandardCharsets.UTF_8);
    }

    void Converse() throws Exception{
        System.out.println(this.GetMessage());
        System.out.println("got the msg");
        while(true){
            String MsgToSrvr = br.readLine();
            this.SendMessage(MsgToSrvr);
            System.out.println("Server Says : "+this.GetMessage());
            if(MsgToSrvr.equals("bye")){
                break;
            }
        }
    }

    public static void main(String[] args) throws Exception{
        ClientUDP Client = new ClientUDP();
        Client.ConnectionSetup();
        Client.Converse();
        Client.ConnectionClose();
    }
}

1 个答案:

答案 0 :(得分:1)

我想您的问题是您先打开服务器,然后再打开客户端。

如果您首先打开客户端,它将在第clientSocket.receive(PktFromServer);行中阻塞,以等待服务器发送内容。之后,打开服务器,服务器在启动后立即发送消息,客户端接收到该消息,将其打印出来,然后等待用户在控制台中打印内容。同时,服务器等待客户端发送一些东西。然后客户端等待服务器,服务器等待用户,等等。

但是,如果您首先打开服务器,它将使消息无处发送,因为UDP不需要在发送内容之前建立连接。在那之后,您打开客户端开始等待服务器,但是它不会收到任何消息,因为来自服务器的初始消息丢失了。因此,他们将坚持等待对方发送邮件。

这是您遇到问题的原因。您没有提到您希望客户端和服务器如何互相配合,所以我现在什么也没建议。您可以指定它,我将建议您进行修改。