我用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条答案
按热度按时间e4eetjau1#
我猜你的问题是你先打开服务器,然后打开客户端。
如果你先打开客户端,它就会阻塞线路
clientSocket.receive(PktFromServer);
正在等待服务器发送内容。之后,打开服务器,它在启动后立即发送消息,客户机接收消息,打印出来,然后等待用户在控制台中打印一些内容。同时,服务器等待客户端发送一些内容。然后客户机等待服务器,服务器等待用户,等等。但是,如果您首先打开服务器,它会将消息吐到任何地方,因为udp不需要在发送某些内容之前建立连接。之后,您打开客户端,它开始等待服务器,但它不会接收任何内容,因为来自服务器的初始消息丢失。所以他们会一直等着对方发东西。
这就是你处境的原因。您没有提到您希望客户端和服务器如何相互作用,所以我现在不建议任何操作。你可以具体说明,我建议修改一下。