fix: #204 Change socket connecting to non-blocking mode
This commit is contained in:
@@ -194,6 +194,54 @@ std::string GetIPAddress(const char *hostName)
|
||||
#endif
|
||||
}
|
||||
|
||||
BOOL ConnectWithTimeout(SOCKET sock, SOCKADDR *addr, int timeout_sec=5)
|
||||
{
|
||||
// <20><>ʱ<EFBFBD><CAB1>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
u_long mode = 1;
|
||||
ioctlsocket(sock, FIONBIO, &mode);
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
int ret = connect(sock, addr, sizeof(*addr));
|
||||
if (ret == SOCKET_ERROR)
|
||||
{
|
||||
int err = WSAGetLastError();
|
||||
if (err != WSAEWOULDBLOCK && err != WSAEINPROGRESS)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
// <20>ȴ<EFBFBD><C8B4><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɻ<EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>
|
||||
fd_set writefds;
|
||||
FD_ZERO(&writefds);
|
||||
FD_SET(sock, &writefds);
|
||||
|
||||
timeval tv;
|
||||
tv.tv_sec = timeout_sec;
|
||||
tv.tv_usec = 0;
|
||||
|
||||
ret = select(0, NULL, &writefds, NULL, &tv);
|
||||
if (ret <= 0 || !FD_ISSET(sock, &writefds))
|
||||
{
|
||||
return FALSE; // <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>
|
||||
int error = 0;
|
||||
int len = sizeof(error);
|
||||
getsockopt(sock, SOL_SOCKET, SO_ERROR, (char*)&error, &len);
|
||||
if (error != 0)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// <20>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
||||
mode = 0;
|
||||
ioctlsocket(sock, FIONBIO, &mode);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
BOOL IOCPClient::ConnectServer(const char* szServerIP, unsigned short uPort)
|
||||
{
|
||||
if (szServerIP != NULL && uPort != 0) {
|
||||
@@ -215,7 +263,7 @@ BOOL IOCPClient::ConnectServer(const char* szServerIP, unsigned short uPort)
|
||||
m_ServerAddr.sin_port = htons(port);
|
||||
m_ServerAddr.sin_addr.S_un.S_addr = inet_addr(m_sCurIP.c_str());
|
||||
|
||||
if (connect(m_sClientSocket,(SOCKADDR *)&m_ServerAddr,sizeof(sockaddr_in)) == SOCKET_ERROR)
|
||||
if (!ConnectWithTimeout(m_sClientSocket,(SOCKADDR *)&m_ServerAddr))
|
||||
{
|
||||
if (m_sClientSocket!=INVALID_SOCKET)
|
||||
{
|
||||
@@ -235,7 +283,6 @@ BOOL IOCPClient::ConnectServer(const char* szServerIP, unsigned short uPort)
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><D7BD><EFBFBD>
|
||||
m_sClientSocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (m_sClientSocket == -1) {
|
||||
Mprintf("Failed to create socket\n");
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user