- 论坛徽章:
- 0
|
原帖由 思一克 于 2007-6-15 10:13 发表
你同时监视几个fd?
应该成fd_set中取出来看。
不好意思,我刚刚没有出错是因为没有传入stat参数给fstat,原来认为如果不关心这个值可以不用传,现在把这个参数传进去就报错了,同select一样是:bad file descriptor,是第一个接收过来的fd,但是我的程序中在处理这个fd发送的请求之前并没有关闭这个fd的操作啊.
修改过的Poll函数如下:
- int CMsgServer::Poll()
- {
- int nFdNum, nEvent, nFd, nRet;
- // 将server listen fd加入fdset中
- FD_SET(m_nListenFd, &m_ReadSet);
- //FD_SET(m_nListenFd, &m_ExpSet);
- LogMsg("nListenFd = %d", m_nListenFd);
- struct stat tStat;
- for(int i = 0; i < m_nMaxCon; ++i)
- {
- nFd = m_szConnections[i].nFd;
- if (-1 != nFd && FD_ISSET(nFd, &m_ReadSet))
- {
- printf("%d in readset\n", nFd);
- if (-1 == fstat(nFd, &tStat))
- {
- printf("fstat %d error:%s", nFd, strerror(errno));
- }
- else
- {
- if (!S_ISSOCK(tStat.st_mode))
- {
- printf("fd %d isnot socket", nFd);
- }
- }
- }
- }
- if (-1 == fstat(m_nListenFd, &tStat))
- {
- printf("fstat %d error:%s", m_nListenFd, strerror(errno));
- }
- if (!S_ISSOCK(tStat.st_mode))
- {
- printf("listen fd %d isnot socket", m_nListenFd);
- }
-
- // 轮询fd集合
- while (true)
- {
- //if (0 > (nFdNum = select(m_nMaxFd + 1, &m_ReadSet, NULL, NULL, NULL)))
- LogMsg("maxfd = %d, nListenFd = %d", m_nMaxFd, m_nListenFd);
- printf("maxfd = %d, nListenFd = %d\n", m_nMaxFd, m_nListenFd);
- if (0 > (nFdNum = select(m_nMaxFd + 1, &m_ReadSet, NULL, &m_ExpSet, NULL)))
- {
- if (EINTR == errno)
- {
- continue;
- }
- else
- {
- LogMsg("[%s] [%s] [%d]: select failed: %s, nListenFd = %d, nMaxFd = %d"
- , __FILE__, __FUNCTION__, __LINE__
- , strerror(errno)
- , m_nListenFd
- , m_nMaxFd);
-
- return -1;
- }
- }
- else
- {
- return 0;
- }
- }
- }
复制代码 |
|