Java TCP 客户端不接收从 C# 服务器发送的消息

发布于 2024-12-11 09:06:56 字数 1833 浏览 0 评论 0原文

我正在用 C# 编写一个 tcp 服务器,并用 java 编写相应的客户端。我正在本地主机上测试连接,客户端能够连接到服务器。但是,当我发送消息时,客户端永远不会收到它们。使用调试器我已经验证了stream.Write(...) 是否被执行。知道问题出在哪里吗?

这是 C# 服务器:

        TcpClient client = (TcpClient)cl;
        NetworkStream stream = client.GetStream();

        byte[] msg = new byte[512];
        int bytesRead; 

        while (running)
        {
            while (messages.getCount() > 0)
            {
                String msg = messages.Take();

                if (cmd != null)
                {
                    byte[] bytes = Encoding.UTF8.GetBytes(msg.ToCharArray());

                    try
                    {
                        stream.Write(bytes, 0, bytes.Length);
                        stream.Flush();
                    }
                    catch (Exception e)
                    {

                    }
                }
            }

            Thread.Sleep(1000); 
        }

和 Java 客户端:

public void run() 
{
    try 
    {
        socket = new Socket(address, port);
        in = new BufferedReader( new InputStreamReader( socket.getInputStream() ));
        out = new PrintWriter(socket.getOutputStream());
        running = true;
    } 
    catch (Exception e){
        e.printStackTrace();
        running = false; 
    } 

    String data;
    while(running)
    {
        try 
        {
            data = in.readLine();

            if(data != null)
            {
                processData(data);
            }
        } 
        catch (IOException e) 
        {
            e.printStackTrace();

            running = false; 
            break;
        }
    }

    try 
    {
        socket.close();
        socket = null;
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }

    running = false; 
}

I'm writing a tcp server in c# and corresponding client in java. I'm testing the connection on localhost, and the client is able to connect to the server. However, when I'm sending messages, the client never receives them. Using the debugger I've verified that stream.Write(...) is executed. Any idea what the problem could be?

This is the c# server:

        TcpClient client = (TcpClient)cl;
        NetworkStream stream = client.GetStream();

        byte[] msg = new byte[512];
        int bytesRead; 

        while (running)
        {
            while (messages.getCount() > 0)
            {
                String msg = messages.Take();

                if (cmd != null)
                {
                    byte[] bytes = Encoding.UTF8.GetBytes(msg.ToCharArray());

                    try
                    {
                        stream.Write(bytes, 0, bytes.Length);
                        stream.Flush();
                    }
                    catch (Exception e)
                    {

                    }
                }
            }

            Thread.Sleep(1000); 
        }

And the Java client:

public void run() 
{
    try 
    {
        socket = new Socket(address, port);
        in = new BufferedReader( new InputStreamReader( socket.getInputStream() ));
        out = new PrintWriter(socket.getOutputStream());
        running = true;
    } 
    catch (Exception e){
        e.printStackTrace();
        running = false; 
    } 

    String data;
    while(running)
    {
        try 
        {
            data = in.readLine();

            if(data != null)
            {
                processData(data);
            }
        } 
        catch (IOException e) 
        {
            e.printStackTrace();

            running = false; 
            break;
        }
    }

    try 
    {
        socket.close();
        socket = null;
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }

    running = false; 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

甜嗑 2024-12-18 09:06:57

您正在使用 BufferedReader.readLine()。您的消息字符串是否以 CR、LF 或 CR/LF 结尾?

readLine 会阻塞,直到读取到行终止字符为止。

You're using BufferedReader.readLine(). Are your message strings terminated by a CR, LF, or CR/LF?

readLine blocks until a line-terminating character is read.

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