- 论坛徽章:
- 0
|
现在一个客户端程序接收服务端程序返回的消息,出现时好时坏的情况(tcp连接)\r\n\r\n客户端发送请求包后,用select函数进行探测,如果有数据再recv接收。\r\n相应的代码如下:\r\nint TcpRecv(int sockfd, char *buf, int len, int nFlag)\r\n{\r\n char *ptr ;\r\n int nbytes = 0;\r\n int left;\r\n fd_set fd;\r\n struct timeval timeout;\r\n int i, nFileFlag;\r\n int retv, retry;\r\n\r\n retry = 0;\r\n nbytes = 0;\r\n ptr = buf + nbytes;\r\n while (len)\r\n {\r\n left = len > PACKET ? PACKET : len;\r\n\r\n FD_ZERO(&fd);\r\n FD_SET(sockfd, &fd);\r\n timeout.tv_sec = nFlag;\r\n timeout.tv_usec = 0;\r\n\r\n retv = select(sockfd + 1, &fd, NULL, NULL, &timeout);\r\n\r\n if (retv <= 0)\r\n {\r\n return -2;\r\n }\r\n\r\n if (FD_ISSET(sockfd, &fd))\r\n {\r\n retry = 0;\r\n nbytes = recv(sockfd, ptr, left, 0);\r\n\r\n if ( nbytes < 0 )\r\n {\r\n return -1;\r\n }\r\n ptr += nbytes;\r\n len = len - nbytes;\r\n if (nbytes == 0) break;\r\n }\r\n\r\n }\r\n\r\n return (ptr - buf);\r\n} | \r\n\r\n在客户端能抓到服务端返回的包,但是对于一些包select的结果一直都是0\r\n分析成功和失败的包数据,目前发现有个问题\r\n成功接受到的包,客户端整个发包服务器回包耗时在0.05秒左右,\r\n接收不到的包,客户端整个发包服务器回包耗时在0.005秒左右。\r\n\r\n有哪位兄弟姐妹遇到过这种情况吗?大家帮忙分析下是什么原因,非常感谢! |
|