TCP 客户端/服务器通信仅发送第一条消息?
我正在 java 中设置一个简单的 TCP 客户端服务器交互。
服务器:
服务器是一个用 Java 编写的桌面客户端:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
class TCPServer
{
public static int PORT_NUMBER = 6129;
public static void main(String argv[]) throws Exception
{
String clientMessage;
ServerSocket welcomeSocket = new ServerSocket(PORT_NUMBER);
while (true)
{
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientMessage = inFromClient.readLine();
System.out.println("Received: " + clientMessage);
outToClient.writeBytes("I received this: "+ clientMessage +"\n");
}
}
}
客户端:
客户端是一个通过 TCP 连接到服务器的 Android 应用程序。在客户端中,我有一个方法 sendMessage(String msg)
尝试向服务器发送消息。
public static void sendMessage(String msg) throws IOException
{
if (mainSocket == null)
{
return;
}
if (!mainSocket.isConnected())
{
connectSocket();
}
PrintWriter output = new PrintWriter( mainSocket.getOutputStream());
output.println(msg);
output.flush();
System.out.println(msg);
}
问题是,服务器收到第一条消息,但任何后续消息都不会显示。当我关闭客户端时,突然所有其他消息立即显示在服务器中。
这是服务器看到的内容:
Received: message 1
很长一段时间没有活动...
然后我关闭客户端,
Received: message 2 message 3 message 4 message 5 etc..
我在 sendMessage() 方法中放置了一个 println,并且该方法本身正在被实时调用。
I am setting up a simple TCP Client Server interaction in java.
Server:
The server is a desktop client written in Java:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
class TCPServer
{
public static int PORT_NUMBER = 6129;
public static void main(String argv[]) throws Exception
{
String clientMessage;
ServerSocket welcomeSocket = new ServerSocket(PORT_NUMBER);
while (true)
{
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientMessage = inFromClient.readLine();
System.out.println("Received: " + clientMessage);
outToClient.writeBytes("I received this: "+ clientMessage +"\n");
}
}
}
Client:
The client is an android app that connects to the server with TCP. In the client I have a method sendMessage(String msg)
which attempts to send a message to the server.
public static void sendMessage(String msg) throws IOException
{
if (mainSocket == null)
{
return;
}
if (!mainSocket.isConnected())
{
connectSocket();
}
PrintWriter output = new PrintWriter( mainSocket.getOutputStream());
output.println(msg);
output.flush();
System.out.println(msg);
}
The problem is, the server receives the first message, but any subsequent messages won't show up at all. When I close the client down, all of a sudden all the other messages show up at once in the server.
This is what the server sees:
Received: message 1
No activity for a long time...
Then I shut down the client
Received: message 2 message 3 message 4 message 5 etc..
I put a println in the sendMessage() method, and the method itself is being called in real time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

每次发送消息时,您都需要在客户端显式
close()
您的PrintWriter
。当您读完inFromClient
和写完outToClient
后,服务器端也是如此。另请参阅这个基本示例,他们很好地解释了基本工作流程:
You need to explicitly
close()
yourPrintWriter
on the client side each time you send a message. Same on the server side when you are done readinginFromClient
, and again when you are done writing tooutToClient
.See also this basic example, they explain the basic workflow quite nicely: