简体   繁体   中英

NetworkStream Sleep() issue

I have written a TCP Server, in which number of bytes to read is prefixed in two-bytes header. After reading from the stream and sending the response back to the client, both NetworkStream and TcpClient are disposed. The problem is that the client doesn't seem to receive my response unless I uncomment Thread.Sleep() line. Here is the code:

using (var tcpClient = await tcpServer.AcceptTcpClientAsync())
{
    tcpClient.NoDelay = true;

    using (var stream = tcpClient.GetStream())
    {
        var twoBytesHeader = new TwoByteHeader();
        var headerBuffer = new byte[twoBytesHeader.HeaderLength];

        using (var binaryReader = new BinaryReader(stream, Encoding.ASCII, true))
        {
            headerBuffer = binaryReader.ReadBytes(twoBytesHeader.HeaderLength);

            int newOffset;
            var msgLength = twoBytesHeader.GetMessageLength(headerBuffer, 0, out newOffset);

            var buffer = binaryReader.ReadBytes(msgLength);

            string msgASCII = Encoding.ASCII.GetString(buffer);

            var bufferToSend = await ProcessMessage(msgASCII);

            using (var binaryWriter = new BinaryWriter(stream, Encoding.ASCII, true))
            {
                binaryWriter.Write(bufferToSend);
                binaryWriter.Flush();
            }

            //Thread.Sleep(1000);
        }
    }
}

When Sleep is uncommented, the client receives response and then indicates that client has disconected. I can't figure out the reason of this behaviour

You are assuming that a read will read as many bytes as you specified. Instead, it will read at least one byte. Your code needs to be able to deal with that. BinaryReader.ReadBytes will allow you to read an exact number of bytes and get rid of some of your boilerplate code.

Also, you probably should not use ASCII encoding which is the worst possible encoding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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