3204|3

68

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

winsocket select 函数的设置为读时返回总是0 [复制链接]

我的代码如下
CUDP_CE *pSocket;
        pSocket = (CUDP_CE*)lparam;
        fd_set fdRead;
        int ret;
        TIMEVAL        aTime;
        char * recvBuf=NULL;
        aTime.tv_sec = 1;
        aTime.tv_usec = 0;
        SOCKADDR_IN tmpAddr;
        int tmpRecvLen;
        int recvLen;
        int iErrorCode;
       
        char * recvedBuf = NULL;
        int recvedBufLen;
       
        while (TRUE)
        {
               
        //收到退出事件,结束线程
                if (WaitForSingleObject(pSocket->m_ExitThreadEvent,0) == WAIT_OBJECT_0)
                {
                        break;
                }

                //将set初始化空集合
                FD_ZERO(&fdRead);
               
                //将pSocket->m_UDPSocket套接字添加到集合中
                FD_SET(pSocket->m_UDPSocket,&fdRead);
               
                //调用select函数,判断套接字I/O状态
                ret = select(0,&fdRead,NULL,NULL,&aTime);
               
                if (ret == SOCKET_ERROR)
                {
                        iErrorCode = WSAGetLastError();
                        pSocket->m_OnUdpError(pSocket->m_pOwnerWnd,iErrorCode);
                        break;
                }
               
                if (ret > 0)
                {
                        if (FD_ISSET(pSocket->m_UDPSocket,&fdRead))
                        {
//要执行的代码
}

如果ret = select(0,&fdRead,NULL,NULL,&aTime);中变为ret = select(0,NULL,&fdRead,NULL,&aTime);ret就可以为1 不是零 还有一个现象就是fdRead在 select  执行之后为空 请各位大侠帮忙解决

最新回复

nfds specifies the maximum number of sockets to check. EXAMPLE In this example select waits for data to read from a socket. #include #include #include #include /* This function calls select to wait for data to read from */ /* one of the sockets passed as a parameter.                */ /* If more than 3 seconds elapses, it returns.              */ /* Return value flags. These indicate the readiness of      */ /* each socket for read.                                    */ #define S1READY 0x01 #define S2READY 0X02 waittoread(int s1,int s2) {    fd_set fds;    struct timeval timeout;    int rc, result;       /* Set time limit. */    timeout.tv_sec = 3;    timeout.tv_usec = 0;       /* Create a descriptor set containing our two sockets.  */    FD_ZERO(&fds);    FD_SET(s1, &fds);    FD_SET(s2, &fds);    rc = select(sizeof(fds)*8, &fds, NULL, NULL, &timeout);    if (rc==-1) {       perror("select failed");       return -1;    }    result = 0;    if (rc > 0)    {       if (FD_ISSET(s1, &fds)) result |= S1READY;       if (FD_ISSET(s2, &fds)) result |= S2READY;    }    return result; }   详情 回复 发表于 2008-4-30 16:40
点赞 关注

回复
举报

76

帖子

0

TA的资源

一粒金砂(初级)

沙发
 
你前一个是指读,,后一个是写,,,改天我也把select用在ce上玩玩,,
 
 

回复

67

帖子

0

TA的资源

一粒金砂(初级)

板凳
 
有点难度哦
 
 
 

回复

60

帖子

0

TA的资源

一粒金砂(初级)

4
 
nfds specifies the maximum number of sockets to check.

EXAMPLE


In this example select waits for data to read from a socket.

#include
#include
#include
#include

/* This function calls select to wait for data to read from */
/* one of the sockets passed as a parameter.                */
/* If more than 3 seconds elapses, it returns.              */
/* Return value flags. These indicate the readiness of      */
/* each socket for read.                                    */
#define S1READY 0x01
#define S2READY 0X02

waittoread(int s1,int s2)
{
   fd_set fds;
   struct timeval timeout;
   int rc, result;

      /* Set time limit. */
   timeout.tv_sec = 3;
   timeout.tv_usec = 0;
      /* Create a descriptor set containing our two sockets.  */
   FD_ZERO(&fds);
   FD_SET(s1, &fds);
   FD_SET(s2, &fds);
   rc = select(sizeof(fds)*8, &fds, NULL, NULL, &timeout);
   if (rc==-1) {
      perror("select failed");
      return -1;
   }

   result = 0;
   if (rc > 0)
   {
      if (FD_ISSET(s1, &fds)) result |= S1READY;
      if (FD_ISSET(s2, &fds)) result |= S2READY;
   }

   return result;
}
 
 
 

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

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/6 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表