判断socket真正连接

康州大雁 2015-07-01 10:58:55
我编写如下代码
CAsyncSocket m_sockee;
BOOL CTelnetClient::ConnectSvr(CString strAddrSvr, DWORD intSvrPort)
{
BOOL ret;
if(m_socket)
m_socket.Close();
if(m_socket.Create() == TRUE)
{
ret=m_socket.AsyncSelect(FD_READ | FD_WRITE | FD_CLOSE | FD_CONNECT | FD_OOB);
if (FALSE==ret) AfxMessageBox(_T("create fail"));
}
ret=m_socket.Connect(strAddrSvr, intSvrPort);
if(ret<0)
return FALSE;
return TRUE;
}
我发现尽管并没有真正连通服务器IP地址,可是这个函数却没有返回FALSE。请教高手其中原因?请请教如何判断socket是真正连接成功了?
...全文
164 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿源是少年 2015-07-01
  • 打赏
  • 举报
回复
此socket模型是异步的,不会立即返回结果,你应当重写OnConnect(int nErrorCode )函数,当结果返回时,OnConnect会被调用,nErrorCode就是错误码,错误码查msdn为以下之一: 0 The function executed successfully. WSAEADDRINUSE The specified address is already in use. WSAEADDRNOTAVAIL The specified address is not available from the local machine. WSAEAFNOSUPPORT Addresses in the specified family cannot be used with this socket. WSAECONNREFUSED The attempt to connect was forcefully rejected. WSAEDESTADDRREQ A destination address is required. WSAEFAULT The lpSockAddrLen argument is incorrect. WSAEINVAL The socket is already bound to an address. WSAEISCONN The socket is already connected. WSAEMFILE No more file descriptors are available. WSAENETUNREACH The network cannot be reached from this host at this time. WSAENOBUFS No buffer space is available. The socket cannot be connected. WSAENOTCONN The socket is not connected. WSAENOTSOCK The descriptor is a file, not a socket. WSAETIMEDOUT The attempt to connect timed out without establishing a connection.
shenyi0106 2015-07-01
  • 打赏
  • 举报
回复
不出意外的话,它返回的值应该是PENDING
shenyi0106 2015-07-01
  • 打赏
  • 举报
回复
CAsyncSocket是一个异步socket,connect后会直接返回,然后在OnConnect中通知连接成功,如果没有收到OnConnect通知,那么就表示没连接上,如果你需要判断状态,请增加一个定时器用来判断
lx624909677 2015-07-01
  • 打赏
  • 举报
回复
查下msdn,看下Connect失败的时候返回的到底是什么

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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