UDP客户端-服务器在Java上都在本地机器上,客户端程序没有响应。

huangapple go评论60阅读模式
英文:

UDP Client-Server in Java both on local machine, The Client Program is not responding

问题

Server程序

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();
    }
}

Client程序

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 received");
        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();
    }
}
英文:

I am simulating a basic client-server chat application in Java using UDP protocol, the problem is my client program is not responding to the message sent by the server program, To begin with, the server sends an initiation message to which the client is supposed to read a string from its respective PowerShell terminal and that message is to be sent over to the server program, As I debugged the code I realized that server program was able to send the initiation message but client program was not taking input from its terminal, Can you see with this, forgive me if this sounds a bit silly but I am a beginner in computer network's course. (Both Client and Server run on my local machine)

Server Program

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();
}    
}

Client Program

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

得分: 1

我猜你的问题在于你先启动了服务器,然后才启动客户端。

如果你先启动客户端,它会在代码行clientSocket.receive(PktFromServer); 处被阻塞,等待服务器发送消息。之后你启动服务器,它会在启动后立即发送消息,客户端接收消息,将其打印出来,并等待用户在控制台输入。与此同时,服务器会等待客户端发送消息。然后客户端等待服务器,服务器等待用户,依此类推。

然而,如果你先启动服务器,它会将消息发送到无处,因为UDP在发送消息之前不需要建立连接。之后,你启动客户端,它开始等待服务器,但由于最初从服务器发出的消息丢失了,所以客户端将无法接收任何消息。因此,它们会彼此等待对方发送消息。

这就是你目前情况的原因。由于你没有提及你希望客户端和服务器如何相互行为,所以我现在不会提供建议。你可以具体说明,之后我可以提供建议。

英文:

I guess your problem is that you turned on the server first, and then the client.

If you turn on the client first, it's going to block on line clientSocket.receive(PktFromServer); waiting for the server to send something. After that, you turn on the server, it sends the message right after starting and client receives the message, prints it out and waits for the user to print something in the console. In the meantime, server waits for client to send something. Then client waits for server and server waits for user, etc.

However if you turn on the server first, it spits out the message to nowhere, because UDP does not need to establish connection prior to sending something. After that, you turn on the client which starts waiting for the server, but it will not receive anything because the initial message from server was lost. So they will stuck waiting for each other to send something.

This is the reason of your situation. You didn't mention how you want the client and the server to mutually behave so I don't suggest anything now. You may specify that and I will suggest a modification.

huangapple
  • 本文由 发表于 2020年9月3日 15:55:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63719199.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定