Java 中的 UDP 客户端-服务器都在本地计算机上,客户端程序没有响应
UDP Client-Server in Java both on local machine, The Client Program is not responding
我正在使用 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();
}
}
我猜你的问题是你先开了服务器,再开了客户端。
如果您先打开客户端,它将阻塞在线 clientSocket.receive(PktFromServer);
等待服务器发送内容。之后,你打开服务器,它在启动后立即发送消息,客户端收到消息,打印出来并等待用户在控制台打印一些东西。与此同时,服务器等待客户端发送一些东西。然后客户端等待服务器,服务器等待用户等
但是,如果您先打开服务器,它会将消息发送到任何地方,因为 UDP 不需要在发送内容之前建立连接。之后,您打开开始等待服务器的客户端,但它不会收到任何东西,因为来自服务器的初始消息丢失了。所以他们会卡住等待对方送东西。
这就是你的情况的原因。你没有提到你希望客户端和服务器如何相互行为,所以我现在不建议任何东西。您可以指定,我会建议修改。
我正在使用 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();
}
}
我猜你的问题是你先开了服务器,再开了客户端。
如果您先打开客户端,它将阻塞在线 clientSocket.receive(PktFromServer);
等待服务器发送内容。之后,你打开服务器,它在启动后立即发送消息,客户端收到消息,打印出来并等待用户在控制台打印一些东西。与此同时,服务器等待客户端发送一些东西。然后客户端等待服务器,服务器等待用户等
但是,如果您先打开服务器,它会将消息发送到任何地方,因为 UDP 不需要在发送内容之前建立连接。之后,您打开开始等待服务器的客户端,但它不会收到任何东西,因为来自服务器的初始消息丢失了。所以他们会卡住等待对方送东西。
这就是你的情况的原因。你没有提到你希望客户端和服务器如何相互行为,所以我现在不建议任何东西。您可以指定,我会建议修改。