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

后端 未结 4 1311
暗喜
暗喜 2021-02-04 21:29

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

4条回答
  •  -上瘾入骨i
    2021-02-04 22:10

    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.

提交回复
热议问题