本文介绍了什么是字节的最低数量,会导致Socket.Receive回来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用的应用协议,它指定在第一个4字节的消息的长度指示符。 Socket.Receive将返回尽可能多的数据在此时的协议栈或阻塞,直到数据可用。这就是为什么我们有,直到我们收到了长度指示器的字节数不间断的从套接字读取。如果对方关闭了连接的Socket.Receive将返回0。我明白了这一切。



是否有已被读取的字节数量最少?我想问的原因是从文档似乎完全有可能整个长度指标(4字节)可能不可用时socket.Receive可以返回。然后,我们就必须有不断尝试。这将是更有效的,以尽量减少我们称之为socket.receive,因为它具有在与出缓冲器的复制物的次数。因此,它是更安全的同时获得长度指标得到一个单字节,它是安全的假设,4个字节将始终可用,或者我们应该不断尝试使用偏移变量拿到4个字节?



这是我认为有可能是某种默认的最低水平的原因是,我遇到了一个名为ReceiveLowWater变量,我可以在套接字选项设置varaible来了。但是,这似乎只适用于BSD。 见SO_RCVLOWAT



这是不是真的那么重要,但我想要编写单元测试。我已经包裹标准的.NET的Socket接口后面。


解决方案

NO. Never. What if someone is testing your protocol with, say, telnet and a keyboard? Or over a real slow or busy connection? You can receive one byte at a time or a split "length indicator" over multiple Receive() calls. This isn't unit testing matter, it's basic socket matter that causes problems in production, especially under stressful situations.

Yes, you should. For your convenience, you can use the Socket.Receive() overload that allows you to specify a number of bytes to be read so you won't read too much. But please note it can return less than required, that's what the offset parameter is for, so it can continue to write in the same buffer:

byte[] lenBuf = new byte[4];
int offset = 0;

while (offset < lenBuf.Length)
{       
    int received = socket.Receive(lenBuf, offset, lenBuf.Length - offset, 0);

    offset += received;     

    if (received == 0)
    {
        // connection gracefully closed, do your thing to handle that
    }
}

// Here you're ready to parse lenBuf

That is correct, the "receive low water" flag is only included for backwards compatibility and does nothing apart from throwing errors, as per MSDN, search for SO_RCVLOWAT:

It's a shame, because it can enhance performance. However, as @cdleonard pointed out in a comment, the performance penalty from keeping an offset variable will be minimal, as you'l usually receive the four bytes at once.

这篇关于什么是字节的最低数量,会导致Socket.Receive回来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 20:00