2

在通过套接字(a/sync)发送字节 [] 数据时,我收到了 messagereceived 事件不匹配数据。例如像这样

客户:

2013-05-20 12:03:09.6929|DEBUG|Tcp|SendAsync_Completed: Sent ( ; T A , 59 84 65 13 10  ) to 127.0.0.1:10002 (Table)!
2013-05-20 12:03:09.8619|DEBUG|Tcp|SendAsync_Completed: Sent ( ; T A , 59 84 65 13 10  ) to 127.0.0.1:10002 (Table)!
2013-05-20 12:03:10.0249|DEBUG|Tcp|SendAsync_Completed: Sent ( ; T A , 59 84 65 13 10  ) to 127.0.0.1:10002 (Table)!
2013-05-20 12:03:10.1899|DEBUG|Tcp|SendAsync_Completed: Sent ( ; T A , 59 84 65 13 10  ) to 127.0.0.1:10002 (Table)!
2013-05-20 12:03:10.3459|DEBUG|Tcp|SendAsync_Completed: Sent ( ; T A , 59 84 65 13 10  ) to 127.0.0.1:10002 (Table)!
2013-05-20 12:03:10.5220|DEBUG|Tcp|SendAsync_Completed: Sent ( ; T A , 59 84 65 13 10  ) to 127.0.0.1:10002 (Table)!
2013-05-20 12:03:10.6890|DEBUG|Tcp|SendAsync_Completed: Sent ( ; T A , 59 84 65 13 10  ) to 127.0.0.1:10002 (Table)!
2013-05-20 12:03:10.8630|DEBUG|Tcp|SendAsync_Completed: Sent ( ; T A , 59 84 65 13 10  ) to 127.0.0.1:10002 (Table)!
2013-05-20 12:03:11.0490|DEBUG|Tcp|SendAsync_Completed: Sent ( ; T A , 59 84 65 13 10  ) to 127.0.0.1:10002 (Table)!
2013-05-20 12:03:11.2040|DEBUG|Tcp|SendAsync_Completed: Sent ( ; T A , 59 84 65 13 10  ) to 127.0.0.1:10002 (Table)!
2013-05-20 12:03:11.3680|DEBUG|Tcp|SendAsync_Completed: Sent ( ; T A , 59 84 65 13 10  ) to 127.0.0.1:10002 (Table)!
2013-05-20 12:03:11.5340|DEBUG|Tcp|SendAsync_Completed: Sent ( ; T A , 59 84 65 13 10  ) to 127.0.0.1:10002 (Table)!
2013-05-20 12:03:11.7030|DEBUG|Tcp|SendAsync_Completed: Sent ( ; T A , 59 84 65 13 10  ) to 127.0.0.1:10002 (Table)!
2013-05-20 12:03:11.8600|DEBUG|Tcp|SendAsync_Completed: Sent ( ; T A , 59 84 65 13 10  ) to 127.0.0.1:10002 (Table)!
2013-05-20 12:03:12.0340|DEBUG|Tcp|SendAsync_Completed: Sent ( ; T A , 59 84 65 13 10  ) to 127.0.0.1:10002 (Table)!

服务器:

2013-05-20 12:03:09.6819|DEBUG|Table|TableServer.Cmd = ; T A
2013-05-20 12:03:09.8959|DEBUG|Table|TableServer.Cmd = ; T A ; T
2013-05-20 12:03:10.0799|DEBUG|Table|TableServer.Cmd = A ; T A
2013-05-20 12:03:10.2569|DEBUG|Table|TableServer.Cmd = ; T A
2013-05-20 12:03:10.4750|DEBUG|Table|TableServer.Cmd = ; T A
2013-05-20 12:03:10.6600|DEBUG|Table|TableServer.Cmd = ; T A
2013-05-20 12:03:10.8830|DEBUG|Table|TableServer.Cmd = ; T A ; T
2013-05-20 12:03:11.0790|DEBUG|Table|TableServer.Cmd = A ; T A
2013-05-20 12:03:11.2700|DEBUG|Table|TableServer.Cmd = ; T A
2013-05-20 12:03:11.5090|DEBUG|Table|TableServer.Cmd = ; T A
2013-05-20 12:03:11.7120|DEBUG|Table|TableServer.Cmd = ; T A ; T
2013-05-20 12:03:11.9180|DEBUG|Table|TableServer.Cmd = A ; T A
2013-05-20 12:03:12.1000|DEBUG|Table|TableServer.Cmd = ; T A

我已经尝试了一切。从异步切换到同步发送,发送前线程休眠,但没有任何效果......

4

1 回答 1

3

请参阅http://tiny.cc/io,特别是“网络数据包:您发送的不是(通常)您得到的”。看起来您希望按照以下方式编写:

var blob = new byte[] {59,84,65,13,10};
for(int i = 0 ; i < 10 ; i++)
    network.Write(blob, 0, blob.Length);

然后将其读取为 10 个 5 块。但是,这根本行不通:TCP 是一个流。当您调用 时Read,您将获得“一些数据,至少一个字节或 EOF,最多 {count} 个字节”。协议不知道也不知道发送代码的结构。它可以在源头缓冲,也可以不缓冲。数据包可以合并和拆分。唯一可以保证的是您以相同的顺序获得相同的字节。但不一定在相同的块中。

所以基本上:分离消息是你的工作。在这种情况下,您可以通过读取 , 的标记值来做到这13一点10。在最基本的层面上,这可能是这样的:

    byte[] ReadToNewline()
    {
        var ms = new MemoryStream();
        int val;
        do
        {
            val = netStream.ReadByte();
            if (val < 0) throw EOF();
            if (val == '\r')
            {
                val = netStream.ReadByte();
                if (val == '\n') return ms.ToArray();
                throw new InvalidOperationException("Expected end-of-line");
            }
            ms.WriteByte((byte)val);
        } while (true);
    }

更优雅的解决方案是可能的;我只是从SimpleRedis中获取的。您可能更喜欢在本地读取更大的缓冲区,然后在接收到的缓冲区中循环查找 CR/LF 对 - 注意 CR 可能位于一个“读取”的末尾,而 LF 位于另一个“读取”的开头,等等。

于 2013-05-20T10:56:21.777 回答