TCPClient 有时会收到空字节数组 [英] TCPClient sometimes recieving empty bytes array

查看:21
本文介绍了TCPClient 有时会收到空字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Currently developing a simple message server using sockets and sometimes the TCPClient receives the correct number of bytes but each byte is 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);
        }

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

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

Msg 包含 139 个字节

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

如果我看一下 ProcessData(data); 我可以看到 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"

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"

读取字节数 = 139

ReadBytes = 139

所以正确的字节数似乎是正确的,但数据本身是错误的.什么会导致这种情况?

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

推荐答案

不太可能.

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

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.

另一件事是 TCP 是基于流的.不要期望收到的字节与您发送的缓冲区相匹配.例如,请参阅此问题.

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.

这篇关于TCPClient 有时会收到空字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆