C ++ socket recv为什么这么慢 [英] C++ socket recv why so slowly

查看:488
本文介绍了C ++ socket recv为什么这么慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我遇到一个关于套接字编程函数recv的问题,让我先介绍一下这个问题的背景,现在我有一个客户端和服务器在不同的pc上,客户端是linux操作系统没有任何第3个网络库,这样的ACE,curl ......和服务器是Window OS - 我已经部署了http服务器--Apache,在我的项目中,我将从socket编程发送http post请求到服务器这是一个php网站及时(大约1次2秒),并且帖子数据被Json格式包围,即http Content-Type:application / json,但是当我发送请求时,我从服务器回收了Json格式的响应(在事实上响应数据不是很大< 5 * 1024),但是在接下来,非常缓慢,大约10秒以上,那么我可以完全得到答案,有时候不能任何响应Json,我想原因是因为不合适的buf长度,我试过1024,2048,512 ......,结果很失望,下面是我的recv代码你能帮忙看看吗?提前谢谢,顺便说一句,我在套接字中使用SOCK_STREAM

recently , I meet a question about the function recv of socket programming, let me introduce the background of the question ,
now I have a client and server in different pc , the client is linux OS without any 3th network libray, such ACE , curl ... and the server is Window OS - and I have deployed the http server - Apache , in my project , I will send http post request from socket programming to server which is a php website timely (about 1 time 2 seconds), and the post data is encapluted with Json format , that is the http Content-Type:application/json, but when I send the request , then I recv the response with Json format from server (in fact the response data is not big < 5*1024), but in the next , very slowly , about more than 10 seconds ,then I can get the response wholly , sometimes can not the any response Json , I suppose the reason is bacause the unfit buf length , I tried 1024 , 2048 , 512 ... , and the result is disappointed , the below is my recv code , could you help to see it ? thanks in advance , BTW , I use SOCK_STREAM in the socket

      void Communication::receive(char buffer[], int BUFFER_SIZE){
        static const int small_buf = 512;
        char buf[small_buf];
        bzero(buf,small_buf);
        int length = 0;
        char* pp = buffer;
        do{
            length = recv(socket_,buf,small_buf,0);
             if(length < 0)
                {
                connect_state_ = false;
                break;
                }else if(length == small_buf){
                connect_state_ = true;
                memcpy(pp, buf, small_buf);
                pp = pp+small_buf;  
            }else if(length < small_buf && length > 0)
            {
                connect_state_ = true;
                memcpy(pp, buf, length);
                pp = pp + length;
            }else if(length == 0)
            {
                break;
            }
        }while(length > 0);
//merge the small_buf into a big buffer which is big enough (50*1024), which is one of function's param 
}





我的尝试:



我更改了buf长度,512,1024,2048,即使我更改SOCK_DGRAW以发送json然后recv,但结果相同,recv非常慢,我使用同步与apache服务器中的php网站进行通信



What I have tried:

I changed the buf length , 512, 1024 ,2048 , even I change SOCK_DGRAW to send json and then recv , but the result is same , recv is very very slow , I use sync to communicate with a php website in apache server

推荐答案

你的代码看起来很简陋,就像我见过的许多其他地方一样。您认为缓冲区大小的影响较小。您还应该使用8k缓冲区,这样您就可以一次获得所有数据。



我的提示是您的服务器或网络速度很慢。看看吧。你需要找到瓶颈。尝试在服务器上进行本地提取。



提示2:使用Wireshark调查网络流量。
Your code looks straitforward, as in many other places I have seen. The influence of buffer size is smaller as you think. You should also use a 8k buffer, so you get all data at once.

My tip is that your server or network is slow. Take a look at it. You need to find the bottleneck. Try a local fetch on the server.

Tip 2: Use Wireshark for investigating the network traffic.


你的套接字是否阻塞或非阻塞?



使用阻塞套接字时, recv 调用仅在有可用数据时返回(返回值> 0),发生超时(返回值0)或发生错误(返回值-1)。



您的代码将等待数据,接收数据并再次等待。如果您没有设置超时值,这将是一个无限循环。使用超时值时, recv 调用将在没有收到数据的时间后返回。根据你的描述,这似乎是超时值为10秒的情况。



使用非阻塞套接字,当数据可用时你应该使用某种信令并且在它返回-1时重复调用 recv 并且 errno EAGAIN EWOULDBLOCK



我建议使用非阻塞套接字。使用阻塞套接字时,您必须立即分析收到的数据,以检测是否已收到所有数据,或者还有更多数据。
Is your socket blocking or non-blocking?

With blocking sockets, the recv call will only return when there are data available (return value > 0), a timeout occured (return value 0), or an error occured (return value -1).

Your code will wait for data, receives it, and waits again. This will be an endless loop if you have not set a timeout value. With a timeout value, the recv call will return after that time when no data has been received. According to your description this seems to be the case with a timeout value of 10 seconds.

With non-blocking sockets you should use some kind of signaling when data are available and call recv repeatedly while it returns -1 and errno is EAGAIN or EWOULDBLOCK.

I suggest to use a non-blocking socket. With blocking sockets you have to analyse the received data immediately to detect if all data has been received or there are more to come.


这篇关于C ++ socket recv为什么这么慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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