TCP 客户端/服务器通信仅发送第一条消息?

发布于 11-30 15:19 字数 1853 浏览 2 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

jJeQQOZ52024-12-07 15:19:41

每次发送消息时,您都需要在客户端显式close()您的PrintWriter。当您读完 inFromClient 和写完 outToClient 后,服务器端也是如此。

另请参阅这个基本示例,他们很好地解释了基本工作流程:

但是,基本原理与此程序中的基本相同:

打开套接字。

打开套接字的输入流和输出流。

根据服务器的协议读取和写入流。

关闭流。

关闭套接字。

You need to explicitly close() your PrintWriter on the client side each time you send a message. Same on the server side when you are done reading inFromClient, and again when you are done writing to outToClient.

See also this basic example, they explain the basic workflow quite nicely:

However, the basics are much the same as they are in this program:

Open a socket.

Open an input stream and output stream to the socket.

Read from and write to the stream according to the server's protocol.

Close the streams.

Close the socket.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文