TCPClient 有时会收到空字节数组

TCPClient sometimes recieving empty bytes array

目前正在使用套接字开发一个简单的消息服务器,有时 TCPClient 会收到正确数量的字节,但每个字节都是 0。

这是发件人代码。

        try
        {
            //c.clientSocket.NoDelay = true;

            // Send back an OK                    
            var clientStream = c.clientSocket.GetStream();                

            var Response = JsonConvert.SerializeObject(new Packet("SERVER", c.ClientName, new List<Payload>() { new Payload(MessageLibrary.Commands.OK, null) }));

            var msg = System.Text.Encoding.ASCII.GetBytes(Response);

            clientStream.Write(msg, 0, msg.Length);                
        }
        catch (Exception ex)
        {
            if (ExceptionRaised != null)
                ExceptionRaised(c.ClientName, ex);
        }

响应 = "{\"TimeStamp\":\"2016-03-18T08:15:15.0881326+00:00\",\"Sender\":\"SERVER\",\"Payload\":[{\"Command\":\"OK\",\"CommandValue\":\"\"}],\"Destination\":\"GBCO0101\"}"

消息包含 139 个字节

看来没问题,这是接收码。

    static void OnDataRecieved(IAsyncResult result)
    {
        TcpClient client = result.AsyncState as TcpClient;

        // Get a stream object for reading and writing
        try
        {
            NetworkStream stream = client.GetStream();

            int ReadBytes = stream.EndRead(result);

            if (ReadBytes == 0)
            {
                // Client gone
                Console.WriteLine("Server lost");
            }
            else
            {
                // Translate data bytes to a ASCII string.
                var data = System.Text.Encoding.ASCII.GetString(ClientReadBuffer, 0, ReadBytes);

                ClientReadBuffer = new byte[ClientReadBuffer.Length];

                stream.BeginRead(ClientReadBuffer, 0, ClientReadBuffer.Length, new AsyncCallback(OnDataRecieved), client);

                ProcessData(data);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("lost connection");
            Console.WriteLine(ex.Message);
        }
    }

如果我看一下 ProcessData(data); 我可以看到数据 = "[=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=][=14=]"

读取字节数 = 139

所以正确的字节数似乎是正确的,但数据本身是错误的。这是什么原因造成的?

不太可能。

您真的在第一个 stream.BeginRead() 上使用 ClientReadBuffer(它不包含在上面的代码中)吗?您可能在某个地方没有以相同的方式进行读取。

为什么每次读取都要为它创建一个新实例?浪费资源。重复使用即可。

另一件事是 TCP 是基于流的。不要指望接收到的字节与您发送的缓冲区匹配。例如,参见此 question