如何使用recv()在C ++套接字中回退超过65000个字节 [英] How to recive more than 65000 bytes in C++ socket using recv()

查看:249
本文介绍了如何使用recv()在C ++套接字中回退超过65000个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++在Linux中开发客户端服务器应用程序(TCP)。我想同时发送超过 65,000 个字节。在TCP中,最大包大小为 65,635 个字节。



如何发送整个字节



以下是我在服务器端的代码。

  //从客户端套接字接收消息
if((iByteCount = recv(GetSocketId(),buffer,MAXRECV,MSG_WAITALL))> 0)
{
printf \\\
Received bytes%d\\\
,iByteCount);

SetReceivedMessage(buffer);
return LS_RESULT_OK;
}



如果使用 MSG_WAITALL

解决方案

p>这可能是你的问题与内核套接字缓冲区大小有关。尝试将以下代码添加到代码中:

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

您可能还需要增加一些sysctl变量:

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

但是,注意,依靠TCP来填充整个缓冲区通常是一个坏主意。你应该多次调用recv()。唯一的好理由,你想要接收超过64K是为了提高性能。但是,Linux应该已经具有自动调整功能,可以根据需要逐渐增加缓冲区大小。


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,635 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.

解决方案

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.

这篇关于如何使用recv()在C ++套接字中回退超过65000个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆