没有看到通过TCP

时间:2017-08-29 10:13:16

标签: c# xml sockets networking tcp

我尝试使用我用作TCP客户端的套接字来接收数据,但不知道所有在WireShark中显示的数据都在我的代码中可用。

使用WireShark我可以看到传入的数据包和测试工具(不是由我制作)以我期望的方式显示数据。

当我在下面的示例中打印recData的长度时,它与WireShark中显示的数据长度相关,因此到目前为止一切似乎都很好。

但是,当我想将收到的消息显示为可读字符串(UTF-8编码的XML)时,我只能看到我期望的部分消息。

我使用Socket

            private Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        private byte[] _recieveBuffer = new byte[1024];

        public void Connect()
        {
            try
            {
                sock.Connect(new IPEndPoint(IPAddress.Parse(this.IpAddress), this.Port));
                sock.ReceiveBufferSize = _recieveBuffer.Length;
            }
            catch (Exception ex)
            { }

            sock.BeginReceive(_recieveBuffer, 0, _recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);

        }

        private void ReceiveCallback(IAsyncResult ar)
        {
            int received = sock.EndReceive(ar);

            if (received <= 0)
                return;

            byte[] recData = new byte[received];
            Buffer.BlockCopy(_recieveBuffer, 0, recData, 0, received);

            if (onDataReceived != null)
                onDataReceived(this, new MyNewEventargs(recData));

            Console.WriteLine(Encoding.UTF8.GetString(recData));

            sock.BeginReceive(_recieveBuffer, 0, _recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);                
        }

我收到的数据包: enter image description here

我注意到第二个收到的包(图中突出显示的包)大约是第一个包的两倍。

当我查看软件包的内容时,我可以看到第一个包含1个完整的XML项目,第二个包含2个。

但是,当我将第二个包的内容打印到字符串时,我仍然只看到一个XML项,因此第三个包丢失了。

我期望的完整XML消息如下所示:

<?xml version="1.0" standalone="yes"?>
<Xml>
<Element1 Attirbute1="First item" Attirbute2="0" Attirbute3="0" Attirbute4="0" Attirbute5="0">
    <Element2 Attirbute1="600" Attirbute2="1" Attirbute3="bla"/>
</Element1>
</Xml>
<?xml version="1.0" standalone="yes"?>
<Xml>
<Element1 Attirbute1="Second item" Attirbute2="0" Attirbute3="0" Attirbute4="0" Attirbute5="0">
    <Element2 Attirbute1="600" Attirbute2="1" Attirbute3="bla"/>
</Element1>
</Xml>
<?xml version="1.0" standalone="yes"?>
<Xml>
<Element1 Attirbute1="Third item" Attirbute2="0" Attirbute3="0" Attirbute4="0" Attirbute5="0">
    <Element2 Attirbute1="600" Attirbute2="1" Attirbute3="bla"/>
</Element1>
</Xml>

Console显示的XML消息如下所示:

//First package
<?xml version="1.0" standalone="yes"?>
<Xml>
<Element1 Attirbute1="First item" Attirbute2="0" Attirbute3="0" Attirbute4="0" Attirbute5="0">
    <Element2 Attirbute1="600" Attirbute2="1" Attirbute3="bla"/>
</Element1>
</Xml>

//First item of second package
<?xml version="1.0" standalone="yes"?>
<Xml>
<Element1 Attirbute1="Second item" Attirbute2="0" Attirbute3="0" Attirbute4="0" Attirbute5="0">
    <Element2 Attirbute1="600" Attirbute2="1" Attirbute3="bla"/>
</Element1>
</Xml>

正如您所看到的,第三个XML项目已丢失。

我尝试使用线程和秒表来查看它是否是时间问题,但由于收到的包的大小是正确的,我不认为这是我的问题的原因。

当我将收到的byte[]打印到控制台时,我还没有完成任何反序列化。

如何让这个套接字接收(或让我看到)发送的所有数据?

编辑:

经过一些调试后,我发现我似乎得到了所有数据,但我认为Encoding.UTF8.GetString(recData)出了问题。

第二次调用ReceiveCallback时,接收的字节数为446而不是221.数据被复制到byte [] recData,它确实保存了446个字节。当我在这一行上设置断点并手动将int received更改为221(因此它只将第一项复制到byte [])时,我得到的结果与它保持在446时相同。

所以我确实获得了所有数据,但不知怎的,它并没有解码到我期望的字符串中。

希望这有道理..

1 个答案:

答案 0 :(得分:0)

你的“onDataReceived”在做什么?什么可能影响你的逻辑?

除了上面提到的事件处理程序之外,你的代码在这里顺利运行(至少使用通用HTML进行测试),接收和显示的所有字节。

(注意:不能发表评论,这就是我发布回复的原因。随意删除。)

下进行。