简体   繁体   English

使用TCP套接字接收无效数据

[英]Receive invalid data with TCP socket

this is my server side code for sending data to client: 这是我的服务器端代码,用于向客户端发送数据:

public bool SendMessage(Socket socket, byte[] message, string logMessage = "Unknow")
    {
        try
        {
            MsgTemp msg = new MsgTemp(socket, logMessage, message);

            System.Diagnostics.Trace.WriteLine(" send ---------- " + message.Length.ToString());

            socket.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(SendCallBack), msg /* null*/);
            return true;
        }
        catch
        {
            // :) removed
        }
        return false;
    }

and this code is receive callback method in client: 并且此代码是客户端中的接收回调方法:

private void ReceiverCallBack(IAsyncResult ar)
    {
        try
        {
            int size = _socket.EndReceive(ar);
            _socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiverCallBack), null);

            System.Diagnostics.Trace.WriteLine(" received ---------- " + size.ToString());

            // messagereceived is an event
            if (MessageReceived != null)
            {
                byte[] temp = new byte[size];
                Array.Copy(_buffer, temp, size);

                MessageReceived(temp);
            }
        }
        catch (Exception exp)
        {
            // :) removed
        }

    }

when server send many packages to client data goes invalid. 当服务器向客户端发送许多数据包时无效。 (in normal/low transaction work correctly) (在正常/低交易中正常工作)

see this (bytes was sent between server an client in a conversation) : 看到此信息(在会话中服务器与客户端之间发送字节):

send ---------- 496 
received ---------- 496
send ---------- 613
received ---------- 613
send ---------- 680
received ---------- 680
send ---------- 227
send ---------- 697
received ---------- 227
send ---------- 722
send ---------- 710
received ---------- 697
received ---------- 1432

two last packages was sent to client received in one package -> 722 + 710 = 1432 and data goes invalid/unusable... 最后两个软件包被发送到一个软件包中接收到的客户端-> 722 + 710 = 1432,数据变为无效/不可用...

why ? 为什么呢? thanks 谢谢

This is how TCP works - it's a stream protocol. 这就是TCP的工作方式-它是协议。 The bytes are guaranteed to be received in exactly the order they are sent, but you can Receive the data of multiple Send operations at once, or receive the data of one Send operation in two Receive s. 保证字节的发送顺序完全相同,但是您可以一次Receive多个Send操作的数据,也可以在两个Receive一个Send操作的数据。

You'll have to somehow define what a message is - it's common to first send the size of a message, followed by the message payload. 您必须以某种方式定义消息是什么-通常首先发送消息的大小,然后发送消息有效负载。

See for example http://blog.stephencleary.com/2009/04/message-framing.html - it contains a good explanation of the why, the how and code examples. 参见例如http://blog.stephencleary.com/2009/04/message-framing.html-它很好地解释了为什么,如何和代码示例。 There are many other examples available online. 在线上还有许多其他示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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