views:

241

answers:

5

Is there any limit on the size of data that can be received by TCP client. With TCP socket communication, server is sending more data but the client is only getting 4K and stopping.

+3  A: 

I'm guessing that you're doing exactly 1 Send and exactly 1 Receive.

You need to do multiple reads, there is no guarantee that a single read from the socket will contain everything.

The Receive method will read as much data as is available, up to the size of the buffer. But it will return when it has some data so your program can use it.

Brian R. Bondy
+1 for probably being correct given a dearth of details to work with. The other, less likely possibility is that they aren't doing a final flush after the last send and the Nagle algorithm is taking its sweet time to send the rest.
Kennet Belenky
A: 

No, it should be fine. I suspect that your code to read from the client is flawed, but it's hard to say without you actually showing it.

Jon Skeet
A: 

No limit, TCP socket is a stream.

Nikolai N Fetissov
A: 

There's no limit for data with TCP in theory BUT since we're limited by physical resources (i.e memory), implementors such as Microsoft Winsock utilize something called "tcp window size".

That means that when you send something with the Winsock's send() function for example (and didn't set any options on the socket handler) the data will be first copied to the socket's temporary buffer. Only when the receiving side has acknowledged that he got that data, Winsock will use this memory again.

So, you might flood this buffer by sending faster than it frees up and then - error!

Poni
i think this is the main problem. are we supposed to change the registry keys to fix this problem ?
Kubi
A: 

You may consider splitting your read/writes over multiple calls. I've definitely had some problems with TcpClient in the past. To fix that we use a wrapped stream class with the following read/write methods:

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.
Robert Davis