超时连接()不起作用
问题描述:
我正在使用此代码连接到服务器,但它并未等待我设置为超时的10秒。它在失败连接后立即返回。超时连接()不起作用
BOOL Connect(string server, int port, int timeout)
{
struct sockaddr_in RemoteHost;
TIMEVAL Timeout;
Timeout.tv_sec = timeout;
Timeout.tv_usec = 0;
int con_error = 0;
#ifdef W32
WSADATA wsd;
if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
{
DEBUG(L"Failed to load Winsock!\n");
return FALSE;
}
#endif
//create socket if it is not already created
if (s == SOCKET_ERROR)
{
//Create socket
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == SOCKET_ERROR)
{
DEBUG(L"Could not create socket");
return FALSE;
}
}
//setup address structure
if (inet_addr(server.c_str()) == INADDR_NONE)
{
struct hostent *he;
//resolve the hostname, its not an ip address
if ((he = gethostbyname(server.c_str())) == NULL)
{
//gethostbyname failed
DEBUG(L"gethostbyname() - Failed to resolve hostname\n");
return FALSE;
}
}
else//plain ip address
{
RemoteHost.sin_addr.s_addr = inet_addr(server.c_str());
}
RemoteHost.sin_family = AF_INET;
RemoteHost.sin_port = htons(port);
//set the socket in non-blocking
unsigned long iMode = 1;
int iResult = ioctlsocket(s, FIONBIO, &iMode);
if (iResult != NO_ERROR)
{
DEBUGP(L"ioctlsocket failed with error: %ld\n", iResult);
return FALSE;
}
//Connect to remote server
if ((con_error=connect(s, (struct sockaddr *)&RemoteHost, sizeof(RemoteHost))) < 0)
{
if (con_error != EINPROGRESS)
{
DEBUG(L"connect() failed");
return FALSE;
}
}
// restart the socket mode
iMode = 0;
iResult = ioctlsocket(s, FIONBIO, &iMode);
if (iResult != NO_ERROR)
{
DEBUGP(L"ioctlsocket failed with error: %ld\n", iResult);
return FALSE;
}
fd_set Write, Err;
FD_ZERO(&Write);
FD_ZERO(&Err);
FD_SET(s, &Write);
FD_SET(s, &Err);
// check if the socket is ready
select(0, NULL, &Write, &Err, &Timeout);
if (FD_ISSET(s, &Write))
{
return TRUE;
}
return FALSE;
}
答
使用WSAGetLastError
找出原因则调用失败。 connect
成功时返回0,否则返回SOCKET_ERROR
。
你的评论是WSAGetLastError
回报WSAEWOULDBLOCK
其中规定:从无法立即完成非阻塞套接字操作返回
此错误,例如recv的时候没有数据正在排队等待从套接字读取。这是一个非致命错误,操作应该稍后重试。 WSAEWOULDBLOCK作为在非阻塞SOCK_STREAM套接字上调用connect的结果被报告是正常的,因为要建立连接需要一定的时间。
因此,您设置的非阻塞套接字的预期行为。
答
当插座无法使用在目标系统上,它可能会发送回an ICMP message indicating that the socket is not open并尝试连接失败。在这种情况下,winsock功能将立即返回 - 这是设计。
使用[WSAGetLastError(http://msdn.microsoft.com/en-us/library/windows/desktop/ms741580%28v=vs.85%29.aspx)获得错误代码,希望这将提供更多信息。 – Steve
@Steve谢谢!它返回WSAEWOULDBLOCK,这似乎等同于* nix EINPROGRESS。它解决了这个问题。再次感谢! – Cornwell
那么,你明确地设置非阻塞,甚至陌生人,它评论:'/ /套接字在非阻塞'。你对非阻塞行为感到惊讶吗? –