What is the minimum number of bytes that will cause Socket.Receive to return?

后端 未结 2 1366
故里飘歌
故里飘歌 2020-12-19 23:52

We are using a application protocol which specifies the length indicator of the message in the first 4 bytes. Socket.Receive will return as much data as in the protocol stac

相关标签:
2条回答
  • 2020-12-20 00:24

    is it safe to assume that 4 bytes will always be available

    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.

    or should we keep trying to get 4 bytes using an offset variable?

    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
    

    The reason that I think that there may be some sort of default minimum level is that I came across a varaible called ReceiveLowWater variable that I can set in the socket options. But this appears to only apply to BSD.

    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:

    This option is not supported by the Windows TCP/IP provider. If this option is used on Windows Vista and later, the getsockopt and setsockopt functions fail with WSAEINVAL. On earlier versions of Windows, these functions fail with WSAENOPROTOOPT". So I guess you'll have to use the offset.

    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.

    0 讨论(0)
  • 2020-12-20 00:24

    No, there isn't a minimum buffer size, the length in the receive just needs to match the actual space.

    If you send a length in four bytes before the messages actual data, the recipient needs to handle the cases where 1, 2, 3 or 4 bytes are returned and keep repeating the read until all four bytes are received and then repeat the procedure to receive the actual data.

    0 讨论(0)
提交回复
热议问题