非阻塞套接字 WSAEINVAL 错误是什么导致的?

unhappyless 2008-12-17 10:12:01

for(;;)
{
//连接服务器
reVal = connect(sClient, (const sockaddr*)&serAddr, sizeof(serAddr));
//处理连接错误
if(SOCKET_ERROR == reVal)
{
int nErrCode = WSAGetLastError();
if(WSAEWOULDBLOCK == nErrCode ||
WSAEINVAL == nErrCode) //如果不加这个,下面会因为 WSAEALREADY 返回FALSE
{
continue;
}else if(WSAEISCONN == nErrCode)
{
break;
}else
{
// WSAEALREADY
//未知原因
return FALSE;
}
}

if(0 == reVal) //连接成功
break;
}
...全文
424 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
arong1234 2008-12-20
  • 打赏
  • 举报
回复
从名字哪里看得出你参数到底对不对?如果名字、类型都不对,就是语法错误,不是运行错误了。这里肯定某个参数值错了
BTW:对于非阻塞socket,WSAEWOULDBLOCK确实是必须检测的,这是表示“正确”的错误码,不是表示“异常”的错误码
[Quote=引用 7 楼 unhappyless 的回复:]
int nErrCode = WSAGetLastError();

为什么这里会发生 WSAEINVAL ?

reVal = connect(sClient, (const sockaddr*)&serAddr, sizeof(serAddr));

参数也没有不正确
[/Quote]
unhappyless 2008-12-20
  • 打赏
  • 举报
回复
int nErrCode = WSAGetLastError();

为什么这里会发生 WSAEINVAL ?

reVal = connect(sClient, (const sockaddr*)&serAddr, sizeof(serAddr));

参数也没有不正确
keven1868 2008-12-20
  • 打赏
  • 举报
回复
Invalid argument.
Some invalid argument was supplied (for example, specifying an invalid level to the setsockopt function). In some instances, it also refers to the current state of the socket—for instance, calling accept on a socket that is not listening.
unhappyless 2008-12-17
  • 打赏
  • 举报
回复
再改下,debug 下几乎100% 发生WSAEALREADY ,release 下有50%概率

这问题咋解决?
unhappyless 2008-12-17
  • 打赏
  • 举报
回复
再次发现 加不加 WSAEINVAL == nErrCode,和下面 WSAEALREADY 错误无关。
WSAEALREADY 在 debug 模式下发生概率极高,release 下没遇到
unhappyless 2008-12-17
  • 打赏
  • 举报
回复
我想知道 WSAEINVAL 发生在什么时候?
yuyunliuhen 2008-12-17
  • 打赏
  • 举报
回复
reference MSDN

Notes for IrDA Sockets
The Af_irda.h header file must be explicitly included.
If an existing IrDA connection is detected at the media-access level, WSAENETDOWN is returned.
If active connections to a device with a different address exist, WSAEADDRINUSE is returned.
If the socket is already connected or an exclusive/multiplexed mode change failed, WSAEISCONN is returned.
If the socket was previously bound to a local service name to accept incoming connections using bind, WSAEINVAL is returned. Note that once a socket is bound, it cannot be used for establishing an outbound connection.
yuyunliuhen 2008-12-17
  • 打赏
  • 举报
回复
没遇到过 一般就这样

if ( connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {
printf( "Failed to connect.\n" );
WSACleanup();
return;
}


UP

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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