TCP Socket Communication Limit

Is there a limit on the size of data that can be received by the TCP client. When connecting TCP sockets, the server sends more data, but the client only receives 4K and stops.

+3
source share
5 answers

You might consider splitting read / write into multiple calls. I have definitely had problems with TcpClientin the past. To fix this, we use a wrapped stream class with the following methods read/write:

public override int Read(byte[] buffer, int offset, int count)
{
    int totalBytesRead = 0;
    int chunkBytesRead = 0;
    do
    {
        chunkBytesRead = _stream.Read(buffer, offset + totalBytesRead, Math.Min(__frameSize, count - totalBytesRead));
        totalBytesRead += chunkBytesRead;
    } while (totalBytesRead < count && chunkBytesRead > 0);
    return totalBytesRead;
}

    public override void Write(byte[] buffer, int offset, int count)
    {
        int bytesSent = 0;
        do
        {
            int chunkSize = Math.Min(__frameSize, count - bytesSent);
            _stream.Write(buffer, offset + bytesSent, chunkSize);
            bytesSent += chunkSize;
        } while (bytesSent < count);
    }

//_stream is the wrapped stream
//__frameSize is a constant, we use 4096 since its easy to allocate.
+2
source

I assume that you are doing exactly 1 Sendand exactly 1 Receive.

, , .

Receive , , . , , .

+7

, . , , , .

+2

, TCP- - .

+2

TCP. , (, ), , Microsoft Winsock, -, tcp.

This means that when you send something using the Winsock send () function, for example (and did not set any parameters in the socket handler), the data is first copied to the temporary socket buffer. Only when the receiving party confirmed that he received this data, Winsock will again use this memory.

So, you can flood this buffer by sending it faster than it is freed, and then an error!

+1
source

Source: https://habr.com/ru/post/1739495/


All Articles