c# - 如何要求套接字等待更多数据到来

标签 c# .net sockets r

我正在玩 RserveCLI项目,它是一个与统计环境 R 通信的 .net 客户端。基本思想是通过 TCP 协议(protocol)在此 .NET 客户端和 R session 之间发送数据/命令。

我和其他人发现的一个错误是大数据主干,比如超过 10k 字节,无法成功传输。我在以下代码片段中找到了 but:

        // send the commend to R, then R will do some computation and get the data ready to send back
        int toConsume = this.SubmitCommand(cmd, data); 
        var res = new List<object>();
        while (toConsume > 0)
        {
            var dhbuf = new byte[4];
            if (this.socket.Receive(dhbuf) != 4)
            {
                throw new WebException("Didn't receive a header.");
            }

            byte typ = dhbuf[0];

            // ReSharper disable RedundantCast
            int dlength = dhbuf[1] + (((int)dhbuf[2]) << 8) + (((int)dhbuf[3]) << 16);

            // ReSharper restore RedundantCast
            var dvbuf = new byte[dlength];

            // BUG: I added this sleep line, without this line, bug occures
            System.Threading.Thread.Sleep(500);

            // this line cannot receive the whole data at once
            var received = this.socket.Receive(dvbuf);
            // so the exception throws 
            if (received != dvbuf.Length)
            {
                var tempR = this.socket.Receive(dvbuf);
                throw new WebException("Expected " + dvbuf.Length + " bytes of data, but received " + received + ".");
            }

原因是.NET代码运行速度太快,R端无法快速发送数据。所以我插入的 Thread.Sleep(500) 之后的接收线没有得到所有的数据。如果我在那里等一段时间,它就可以获得所有数据。但我不知道要多久。

我有一些基本的思路来处理这个问题,比如一直用this.socket.Receive()去获取数据,但是如果那里没有数据,.Receive就会阻塞在那里。

我对套接字编程经验不多,所以我问这种问题的最佳实践。谢谢!

最佳答案

根据docs :

If you are using a connection-oriented Socket, the Receive method will read as much data as is available, up to the size of the buffer.

因此,您永远无法保证获得接收调用中要求的所有数据。您需要检查 Receive 实际返回了多少字节,然后对剩余字节发出另一个接收调用。继续该循环,直到获得所有要查找的字节。

关于c# - 如何要求套接字等待更多数据到来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8081444/

相关文章:

c# - 寻找 Silverlight/.NET 的技术和/或框架的良好总结

c# - 从数据库获取所有数据并在客户端进行排序,这是最佳的还是理想的?

.NET 复制保护

php - PHP将整数转换为32位(4字节)十六进制以进行套接字编程

c# - JVM (Java) 或 CLR (C#) 中的运行时异常处理(如除以零)

C# 4.0,检测方法是否缺失

c# - 替代字典进行快速键查找?

c# - 直接修改 List<T> 元素

python - 套接字使用随机端口,我需要一个

java - Java SSLSocket 问题