免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1253 | 回复: 2
打印 上一主题 下一主题

select 的奇怪问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-04-20 15:13 |只看该作者 |倒序浏览
for (; {
                /* waiting incoming message from port 4000 */

                t = 0;
                n = 0;

                FD_ZERO(&fdR);
                FD_SET(sockfd_dvs, &fdR);
                memset(&tv, 0, sizeof(struct timeval));
                tv.tv_sec = 5;
                tv.tv_usec = 0;

                 t = select(sockfd_dvs + 1, &fdR, NULL, NULL, &tv);
                //DPRINT("select return t = %d, tv_sec = %d, tv_usec = %d\n", tv.tv_sec, tv.tv_usec);

                if (t > 0 && FD_ISSET(sockfd_dvs, &fdR)) {

                        static char *pmsgstart = NULL, *p1 = NULL, *p2 = NULL;

                        /*receive message from network*/
                        memset(&saddr, 0, sizeof(struct sockaddr_in));
                        n = 0;
                        len = sizeof(struct sockaddr_in);
                        n = recvfrom(sockfd_dvs, &RecvMsg[0], MAX_MSG_LENGTH, 0,
                                     (struct sockaddr*)&saddr, &len);

                        RecvMsg[n] = '\0';
                        DPRINT("sin_add = %u, sin_port = %u\n",saddr.sin_addr, saddr.sin_port);
                        DPRINT("the sockfd_dvs got a message [%s] from %s:%d\n",
                                RecvMsg, inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));

                        //sendto(sockfd_dvs, RecvMsg, MAX_MSG_LENGTH, 0, (struct sockaddr*)&saddr, len);
                        FD_CLR(sockfd_dvs, &fdR);
                        /*analyse the message body, and find out the information that we want to get*/
                }/*if ISSET(sockfd_dvs)*/
        }/*for*/


奇怪的问题,我使用select 作为服务器端来接收UDP, 但只能接受每个客户端的第一个包(如果有多个客户端),客户端的第二个包的信息就收不到(select 没有检测到可读句柄), 但如果在recvfrom()函数后,调用sendto()到相同的客户端,则接受同一客户端的数据包正常,我怀疑是否是sockfd 被组塞在 write 下,但我不需要做sendto, 我该如何解决这个问题。

谢谢!

论坛徽章:
0
2 [报告]
发表于 2007-04-21 20:07 |只看该作者
帮你顶一下~

论坛徽章:
0
3 [报告]
发表于 2007-04-23 18:28 |只看该作者

has fixed it.

thanks, the issue has be fixed, the issue really from my code ( my client program, client will read the socketfd_client after sent a message to server, And I have not use the select in client program, so the client will be blocked on read(), so that the server need to send a message to client , then client can continue the write()), the code is very ungainliness.

So , please friends notice the result, and you don't make same error with me.

Thanks.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP