TCPClient sometimes recieving empty bytes array

 ̄綄美尐妖づ 提交于 2020-01-14 05:25:27

问题


Currently developing a simple message server using sockets and sometimes the TCPClient receives the correct number of bytes but each byte is 0.

Here's the sender code.

        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);
        }

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

Msg contains 139 bytes

So this seems ok, here is the receiving code.

    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);
        }
    }

If I take a look at ProcessData(data); I can see that data = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"

ReadBytes = 139

So the right amount of bytes seems to be correct but the data itself is wrong. What can cause this?


回答1:


It's unlikely.

Are you really using ClientReadBuffer on the first stream.BeginRead() (it's not included in the code above)? You probably have one somewhere that doesn't do the read in the same way.

And why do you create a new instance of it for every read? Waste of resources. Just reuse it.

Another thing is that TCP is stream based. Don't expect the bytes received to match the buffer that you sent. See this question for instance.



来源:https://stackoverflow.com/questions/36079517/tcpclient-sometimes-recieving-empty-bytes-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!