为什么select返回-1

「已注销」 2009-03-07 10:21:19
模仿别人的程序,客户端和服务器通信,但是老是连接上了,信息发送不过去,后来调试的时候是在使用select()的时候它返回了-1,直接跳出了程序,没有发送数据,代码为:
BOOL CDealCMDThread::SOKCET_Select(SOCKET hSocket,int nTimeOut,BOOL bWrite)
{
fd_set fdset;
struct timeval tv;
FD_ZERO(&fdset);
FD_SET(hSocket, &fdset);
tv.tv_sec = nTimeOut;
tv.tv_usec = 0;

int nRes = 0;
if(bWrite)
nRes = select(hSocket + 1, NULL , &fdset, NULL, &tv);
else
nRes = select(hSocket + 1, &fdset, NULL , NULL, &tv);

if(nRes == 0)
{
// DW(_T("SOKCET_Select Time Out"));
return FALSE;
}

if(nRes < 0)
{
int nErr = WSAGetLastError();
// DW(_T("SOKCET_Select Select Error = %d"),nErr);
return FALSE;
}

if(FD_ISSET(hSocket, &fdset))
return TRUE;

return FALSE;
}
为什么nRes的返回值为-1啊,返回-1代表什么出错呢??
请各位帮帮我!
...全文
1672 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wjj305590 2011-03-24
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 adslhack 的回复:]

select(hSocket + 1, NULL , &fdset, NULL, &tv);

为什么要hSocket + 1 呢?
还是没有明白
[/Quote]
windows下是没用的,为了兼容

The nfds parameter is included only for compatibility with Berkeley sockets.

linux下是最大描述字+1
笨笨狮子 2010-10-26
  • 打赏
  • 举报
回复
select(hSocket + 1, NULL , &fdset, NULL, &tv);

为什么要hSocket + 1 呢?
还是没有明白
「已注销」 2009-03-07
  • 打赏
  • 举报
回复
谢谢你们
oyljerry 2009-03-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 shaochem 的回复:]
也就是我调试的时候nErr的值为10038,这是什么出错啊 ?
[/Quote]

WSAENOTSOCK
(10038)
Socket operation on non-socket.
An operation was attempted on something that is not a socket. Either the socket handle parameter did not reference a valid socket, or for select, a member of an fd_set was not valid.

hSocket无效了...
cnzdgs 2009-03-07
  • 打赏
  • 举报
回复
hSocket无效,看看这个参数是怎么来的。
「已注销」 2009-03-07
  • 打赏
  • 举报
回复
也就是我调试的时候nErr的值为10038,这是什么出错啊 ?
「已注销」 2009-03-07
  • 打赏
  • 举报
回复
错误代码为10038
「已注销」 2009-03-07
  • 打赏
  • 举报
回复
不是那个问题
oyljerry 2009-03-07
  • 打赏
  • 举报
回复
nRes = select(hSocket, NULL , &fdset, NULL, &tv);
  • 打赏
  • 举报
回复
nRes = select(hSocket + 1, NULL , &fdset, NULL, &tv);

为什么要用 hSocket + 1 呢, 不明白, 返回-1表示调用失败,很可能就是这个地方的问题, 是不是应该直接用 hSocket

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧