简体   繁体   English

在 Tcp 通信期间使用 StreamReader

[英]using StreamReader during Tcp communication

I'm trying to learn how to implement TCP communication in C# and I am a bit confused about how a StreamReader knows when a sender is no longer sending data.我正在尝试学习如何在 C# 中实现 TCP 通信,我对 StreamReader 如何知道发送者何时不再发送数据感到有些困惑。

     private async void TakeCareOfTCPClient(TcpClient paramClient)
    {
        NetworkStream stream = null;
        StreamReader reader = null;

        try
        {
            stream = paramClient.GetStream();
            reader = new StreamReader(stream);

            char[] buff = new char[64];

            while (KeepRunning)
            {
                Debug.WriteLine("*** Ready to read");

                int nRet = await reader.ReadAsync(buff, 0, buff.Length);

                System.Diagnostics.Debug.WriteLine("Returned: " + nRet);

                if (nRet == 0)
                {
                    RemoveClient(paramClient);   //This removes the client from a list containing 
                                                 // all connected clients to the server.

                    System.Diagnostics.Debug.WriteLine("Socket disconnected");
                    break;
                }

                string receivedText = new string(buff);

                System.Diagnostics.Debug.WriteLine("*** RECEIVED: " + receivedText);

                Array.Clear(buff, 0, buff.Length);


            }

        }
        catch (Exception excp)
        {
            RemoveClient(paramClient);
            System.Diagnostics.Debug.WriteLine(excp.ToString());
        }

    }

Let us say that the client sends a message that is exactly 64 characters long.假设客户端发送的消息长度正好为 64 个字符。 However, the client doesn't disconnect from the server and could send another message at a later time (let's assume a large amount of time passes between messages).但是,客户端不会断开与服务器的连接,并且可以在稍后发送另一条消息(假设消息之间经过大量时间)。 Will the Server that has called this method immediately attempt another read and remove the client from its list of connections because the client hasn't sent another message yet (ie 0 characters are read)?调用此方法的服务器是否会立即尝试再次读取并将客户端从其连接列表中删除,因为客户端尚未发送另一条消息(即读取了 0 个字符)? On the assumption that it doesn't, what is it about the stream (perhaps a special character it contains or a hidden status it has) that causes the server to wait for another message and not return 0?假设它没有,那么导致服务器等待另一条消息而不返回 0 的 stream (可能是它包含的特殊字符或隐藏状态)是什么?

So that code is stopping when reader.ReadAsync returns 0 .因此,当reader.ReadAsync返回0时,该代码将停止。

The Remarks section of the documentation on Stream.Read holds the key to when that happens: Stream.Read文档的备注部分掌握了何时发生这种情况的关键:

Read returns 0 only when there is no more data in the stream and no more is expected (such as a closed socket or end of file).仅当 stream 中没有更多数据并且预计不会有更多数据(例如关闭的套接字或文件结尾)时,读取才会返回 0。

So, since the underlying stream is a TCP connection, ReadAsync will return 0 (and TakeCareOfTCPClient will return) when the TCP connection is closed.因此,由于底层 stream 是一个 TCP 连接,当 TCP 连接关闭时, ReadAsync将返回0 (并且TakeCareOfTCPClient将返回)。 Either side of the connection can close the connection.连接的任何一方都可以关闭连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM