gpt4 book ai didi

java - TCP/IP 客户端向服务器发送多条消息

转载 作者:行者123 更新时间:2023-12-02 02:26:51 26 4
gpt4 key购买 nike

我正在使用这段代码在客户端和服务器之间进行通信,现在我需要并且正在努力解决的是如何使服务器接受来自客户端的多个消息;并且客户端和服务器之间的通信不是 1 个客户端消息然后 1 个服务器消息,而是可以按每个顺序写入。

我该怎么做?

谢谢

这是我的代码:

public class GossipServer {

public static void main(String[] args) throws Exception{
//create a server socket
ServerSocket sersock = new ServerSocket(3000);
System.out.println("Server ready for chatting");
Socket sock = sersock.accept();
System.out.println("Client accepted information"+sock.getInetAddress()+sock.getPort());

// reading from keyboard (keyRead object)
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
System.out.println(""+keyRead.toString());

// sending to client (pwrite object)
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);
System.out.println("output stream "+ostream.toString());

// receiving from client ( receiveRead object)
InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
System.out.println("input stream "+istream.toString());

//receiving message. send message
String receiveMessage, sendMessage;
while(true) {
if((receiveMessage = receiveRead.readLine()) != null ) {
System.out.println(receiveMessage); }

sendMessage = keyRead.readLine();
pwrite.println(sendMessage);
pwrite.flush();

}
}
}
}

客户端:

public class GossipClient {

public static void main(String[] args) throws Exception {
Socket sock = new Socket("127.0.0.1", 3000);
// reading from keyboard (keyRead object)
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
// sending to client (pwrite object)
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);

// receiving from server ( receiveRead object)
InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

System.out.println("Start the chitchat, type and press Enter key");

String receiveMessage, sendMessage;
while(true) {
sendMessage = keyRead.readLine(); // keyboard reading
pwrite.println(sendMessage); // sending to server
pwrite.flush(); // flush the data
if((receiveMessage = receiveRead.readLine()) != null) //receive from server {
System.out.println(receiveMessage); // displaying at DOS prompt
}
}
}
}

最佳答案

首先,这个答案不会是如何解决您的问题的指南,而是更详细地解释您必须解决的问题,包括一些建议和想法。

现在我们来谈谈阻塞操作。例如,使用 BufferedReader::readLine 方法从 InputStream 读取数据就是这样的阻塞操作。这意味着它将阻塞整个线程,直到它返回。把它想象成一个 while 循环。

现在,为了绕过此限制,您可以创建一个新线程,该线程仅监听流并打印出它收到的任何内容。我个人在一些小型项目和测试中采用了这种方法,因为服务器端的每个连接始终需要两个线程。

如果您只想要一个小型的点对点聊天应用程序,我会遵循这种方法,因为它更容易理解(在我看来)并且足够复杂来处理您的问题。

<小时/>

但是,如果您想要一个可容纳数千名用户同时聊天的大型聊天室,我会遵循 java.nio 包中引入的 channel 方法。

但是,由于我还没有深入了解核心 API 的这些领域,因此我无法提供更多具体信息。

关于java - TCP/IP 客户端向服务器发送多条消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57247726/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com