Search code examples
c++linuxsocketstcpserversocket

How to recive more than 65000 bytes in C++ socket using recv()


I am developing a client server application (TCP) in Linux using C++. I want to send more than 65,000 bytes at the same time. In TCP, the maximum packet size is 65,535 bytes only.

How can I send the entire bytes without loss?

Following is my code at server side.

//Receive the message from client socket
if((iByteCount = recv(GetSocketId(), buffer, MAXRECV, MSG_WAITALL)) > 0) 
{
     printf("\n Received bytes %d\n", iByteCount);

     SetReceivedMessage(buffer);
     return LS_RESULT_OK;
}

If I use MSG_WAITALL it takes a long time to receive the bytes so how can I set the flag to receive more than 1 million bytes at time.

Edit: The MTU size is 1500 bytes but the absolute limitation on TCP Packet size if 65,535.


Solution

  • It is possible that your problem is related to kernel socket buffer sizes. Try adding the following to your code:

    int buffsize = 1024*1024;
    setsockopt(s, SOL_SOCKET, SO_RCVBUF, &buffsize, sizeof(buffsize));
    

    You might need to increase some sysctl variables too:

    sysctl -w net.core.rmem_max=8388608
    sysctl -w net.core.wmem_max=8388608
    

    Note however, that relying on TCP to fill your whole buffer is generally a bad idea. You should rather call recv() multiple times. The only good reason why you would want to receive more than 64K is for improved performance. However, Linux should already have auto-tuning that will progressively increase the buffer sizes as required.