Re: Could not receive more than 2KB at TCP/IP communication!

From: Tomas Restrepo \(MVP\) (tomasr_at_mvps.org)
Date: 06/11/04


Date: Fri, 11 Jun 2004 06:34:37 -0500

Alper,

> I faced with a problem during developping a TCP/IP Client application. The
Client application will receive intensive data from Server. Client is
receiving data by using the recv(...) functions, as normal. My code is as
follows:
>
> ----------
> public __gc class Form1 : public System::Windows::Forms::Form
> {
> ....
> public:
> #define Max_Buffer_Size 50000
> SOCKET ClientSocket;
> ......
>
> private:
> void trd_Recv_Task()
> {
> ....
> char* recvBuf = new char[Max_Buffer_Size];
> int retRecvVal = recv(ClientSocket , recvBuf , Max_Buffer_Size ,
0);
> .....
> }
> ....
> }
> ------------
>
> During this receiving, I noticed that I could not receive more than 2048
Bytes (2KB) once at a time. In my first version of code, I was taking data
from Server, then, doing operations depending on the received data. But
once, receiving data were more than 2KB, and I could not take then rest of
my working codes gave erros.
>
> I changed my technique now. First, I receive only a 2 bytes of data that
says me the size of the following receiving in integer format. Then I run
the recv(...) function in a while(...) loop, till the expected number of
bytes are received. The rest is working.

That's exactly how it is meant to be used. You loop until you get all bytes
you need.
The documentation clearly states "For connection-oriented sockets (type
SOCK_STREAM for example), calling recv will return as much information as is
currently available-up to the size of the buffer specified", which implies
that it might very well return less than you asked for. This is perfectly
normal behavior.

-- 
Tomas Restrepo
tomasr@mvps.org


Relevant Pages

  • Re: Howto reset and reuse a socket (not having a 7.80 sec delay)
    ... > The server is receiving data from the client. ... > the new connection is accepted and the old connection closed. ...
    (comp.os.linux.development.apps)
  • Re: Perl Socket question
    ... >I have written a simple client using the above code - does this code block ... >pause a long time when receiving data. ... >the server port directly using telnet and the data sent by the server is ... >sent back to the client (telnet client) much faster than it is to my ...
    (comp.lang.perl.misc)
  • Re: Could not receive more than 2KB at TCP/IP communication!
    ... Client application will receive intensive data from Server. ... receiving data by using the recvfunctions, ... You loop until you get all bytes ...
    (microsoft.public.dotnet.languages.vc)
  • Perl Socket question
    ... I have written a simple client using the above code - does this code block ... pause a long time when receiving data. ... the server port directly using telnet and the data sent by the server is ... sent back to the client (telnet client) much faster than it is to my ...
    (comp.lang.perl.misc)
  • Re: Unable to detect network cable unplugged event
    ... Ankur Saxena wrote: ... Are not able to detect that network connectivity is lost, Client ... cable back they start receiving data again. ...
    (Fedora)