判断socket真个连接
判断socket真正连接
我编写如下代码
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是真正连接成功了?
------解决思路----------------------
CAsyncSocket是一个异步socket,connect后会直接返回,然后在OnConnect中通知连接成功,如果没有收到OnConnect通知,那么就表示没连接上,如果你需要判断状态,请增加一个定时器用来判断
------解决思路----------------------
此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.
我编写如下代码
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是真正连接成功了?
------解决思路----------------------
CAsyncSocket是一个异步socket,connect后会直接返回,然后在OnConnect中通知连接成功,如果没有收到OnConnect通知,那么就表示没连接上,如果你需要判断状态,请增加一个定时器用来判断
------解决思路----------------------
此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.