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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello Developpers,
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.

As a note, I am using MS Visual C++ .NET Standard on Windows 2000 Professional O.S.

I searched over internet about how to resize the recv( ) buffer. I understood that TCPWindowSize is the point that I have to work on. Or there are some other settings that I don't know now.

If you may guide me about resizing the recv(...) buffer, I will be so happy.

Respectfully, yours

Alper
 
setsockopt SO_RCVBUF sets receive buffer size
But your app behaved correctly - with stream-oriented sockets (SOCK_STREAM,
that is when you use IPPROTO_TCP) you receive data as a stream of bytes, so
you don't know particular sizes of each send at server size. It's known whe
using SOCK_DGRAM sockets. If server sends to you 4KB data throught TCP, you
can receive it in 1,2,3,4,5 or 10 different-size parts.
 
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);
.....
}
....
}
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.
 
Back
Top