c#socket发送数据不匹配的服务器数据

时间:2013-05-20 10:48:06

标签: c# tcpclient

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

客户端:

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

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

1 个答案:

答案 0 :(得分: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}个字节”。协议确实不知道,不能知道发送代码的结构。它本来可以在sourse缓冲,或不。数据包可以组合和拆分。所有保证的是,您以相同的顺序获得相同的字节。但不一定是相同的块。

基本上:你的工作来分隔消息。在这种情况下,您可以通过阅读1310的标记值来实现此目的。在最基本的层面上,可能是这样的:

    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位于另一个的开头,等等。 / p>