如何要求套接字等待更多数据来 [英] How to ask the socket to wait for more data to come

查看:83
本文介绍了如何要求套接字等待更多数据来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩 RserveCLI 项目,该项目是与统计环境R通信的.net客户端。基本思想是通过TCP协议在此.NET客户端和R会话之间发送数据/建议。

I am playing with RserveCLI project, which is a .net client communicating with the statistical environment R. The basic idea is sending data/commends between this .NET client and an R session via TCP protocol.

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

One bug that others and I found is that big data trunk, say over 10k bytes, cannot get transfer successfully. I found the but in the following code snippet:

        // 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)之后的接收行无法获取所有数据。如果我在那里等了一段时间,那么它可以获得所有数据。但是我不知道要多久

The reason is that the .NET code runs too fast and the R side cannot send the data that fast. So the receive line after my inserted Thread.Sleep(500) does not get all the data. If I wait some time there, then it can get all the data. But I don't know how long.

我有一些基本的想法可以解决这个问题,例如,连续使用this.socket.Receive()来获取数据,但是如果那里没有数据。接收将在那里阻塞。

I have some basic idea to deal with the problem, for example, continuously use this.socket.Receive() to get data, but if there is no data there .Receive will block there.

我在套接字编程方面经验不足,因此我想询问此类问题的最佳实践。谢谢!

I have little experience in socket programming, so I am asking the best practice for this kind of problem. Thanks!

推荐答案

根据文档


如果您使用的是面向连接的Socket,接收方法将读取尽可能多的可用数据,直到缓冲区的大小。

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.

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

So you are never guranteed to get all the data asked for in the receive call. You need to check how many bytes were actually returned by the Receive, then issue another receive call for the remaining bytes. Continue that loop until you get all the bytes you were looking for.

这篇关于如何要求套接字等待更多数据来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆