簡體   English   中英

Java TCP客戶端未收到從C#服務器發送的消息

[英]Java TCP Client doesn't receive messages sent from C# Server

我正在用C#和Java中的相應客戶端編寫一個TCP服務器。 我正在localhost上測試連接,並且客戶端能夠連接到服務器。 但是,當我發送消息時,客戶端永遠不會收到消息。 使用調試器,我驗證了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; 
}

您正在使用BufferedReader.readLine() 您的消息字符串是否以CR,LF或CR / LF終止?

readLine一直阻塞,直到讀取行結束符為止。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM