为什么加上WSAAsyncSelect后,socket就不能连接服务器了?

f_liu 2014-04-23 12:27:49

#define WM_SOCKET WM_USER+100


if(WSAAsyncSelect(s, this->m_hWnd, WM_SOCKET, FD_READ | FD_ACCEPT | FD_WRITE | FD_CLOSE) == SOCKET_ERROR)
{
MessageBox("注册异步失败!!");
}


if (::connect(s, (sockaddr*)&addr, sizeof(addr)) != SOCKET_ERROR)
{
GetDlgItem(IDC_SHOW)->GetWindowText(str);
str += "连接服务器成功! \r\n";
GetDlgItem(IDC_SHOW)->SetWindowText(str);

GetDlgItem(IDC_Send)->EnableWindow(TRUE);
GetDlgItem(IDC_SendMsg)->EnableWindow(TRUE);
GetDlgItem(IDC_Port)->EnableWindow(FALSE);
GetDlgItem(IDC_Connect)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT1)->EnableWindow(FALSE);
}
else
{
GetDlgItem(IDC_SHOW)->GetWindowText(str);
str += "连接服务器失败!\r\n";
GetDlgItem(IDC_SHOW)->SetWindowText(str);

}


如果把WSAAsyncSelect去掉是可以连接服务器的,加上就一直是连接服务器失败
...全文
156 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
f_liu 2014-04-26
  • 打赏
  • 举报
回复
引用 4 楼 tujiaw 的回复:
在WM_SOCKET消息函数里面判断收到FD_CONNECT通知表示连接成功,不能根据connect返回值来判断
那个问题解决了……问题是现在在服务器中接受不到accept等的消息
ningto.com 2014-04-24
  • 打赏
  • 举报
回复
在WM_SOCKET消息函数里面判断收到FD_CONNECT通知表示连接成功,不能根据connect返回值来判断
ningto.com 2014-04-23
  • 打赏
  • 举报
回复
With a nonblocking socket, the connection attempt cannot be completed immediately. In this case, connect will return SOCKET_ERROR, and WSAGetLastError will return WSAEWOULDBLOCK. In this case, there are three possible scenarios: Use the select function to determine the completion of the connection request by checking to see if the socket is writeable. If the application is using WSAAsyncSelect to indicate interest in connection events, then the application will receive an FD_CONNECT notification indicating that the connect operation is complete (successfully or not). If the application is using WSAEventSelect to indicate interest in connection events, then the associated event object will be signaled indicating that the connect operation is complete (successfully or not).
ningto.com 2014-04-23
  • 打赏
  • 举报
回复
WSAAsyncSelect会将socket设置为非阻塞模式,然后看MSDN上说的connect非阻塞模式下的返回值
赵4老师 2014-04-23
  • 打赏
  • 举报
回复
参考MSDN98\SAMPLES\VC98\SDK\NETDS\WINSOCK\WSOCK\WSOCK.C http://bbs.csdn.net/topics/390374955

65,208

社区成员

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

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