How to can I set the connect()ion timeout time?
Thank you.
Printable View
How to can I set the connect()ion timeout time?
Thank you.
yeah, I've read it. But the problem thatis not supported by winsock.Code:fcntl(sock, F_SETFL, O_NONBLOCK);
Isn't there any other way to set the timeout time?
http://tangentsoft.net/wskfaq/newbie.html, 2.14.
If that doesn't help, then
http://groups.google.com/groups?q=wi...imeout+connect
Quote:
connect() have a timeout
embedded into them. The theory behind this is that only the stack has
all the information necessary to set a proper timeout.
connect()'s timeout value can't be changed... I guess that leaves me with select() and the non blocking option, that I can't even find at the msdn.
EDIT: Cleaned up code for prosperity. Error checking is still non-existant.
Code:int main(void)
{
SOCKET s;
HANDLE hEvent;
ULONG setting;
WSADATA wd;
int cbRecv;
char buf[1024];
struct sockaddr_in sa = { 0 };
WSANETWORKEVENTS ne;
WSAStartup(MAKEWORD (2,2), &wd);
hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
s = socket(AF_INET, SOCK_STREAM, 0);
/* Puts socket in non-blocking mode and
* tells windows to signal hEvent when the
* connect() has completed or an error occurs. */
WSAEventSelect(s, hEvent, FD_CONNECT);
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr("66.35.250.203");
sa.sin_port = htons(80);
connect(s, (struct sockaddr *) &sa, sizeof(struct sockaddr_in));
/* Wait 3 seconds for socket to connect() or error to occur. */
if (WaitForSingleObject(hEvent, 3000) == WAIT_OBJECT_0)
{
WSAEnumNetworkEvents(s, NULL, &ne);
if (ne.iErrorCode[FD_CONNECT_BIT] == 0)
printf("Connected successfully.\n");
else
printf("Failed to connect with error %d.\n", ne.iErrorCode[FD_CONNECT_BIT]);
}
else
{
printf("Timed out.\n");
}
/* Disable event signalling */
WSAEventSelect(s, hEvent, 0);
CloseHandle(hEvent);
/* Put socket back into blocking mode */
setting = 0;
ioctlsocket(s, FIONBIO, &setting);
send(s, "GET / HTTP/1.0\r\n\r\n", sizeof("GET / HTTP/1.0\r\n\r\n") - 1, 0);
cbRecv = recv(s, buf, sizeof(buf) - 1, 0);
closesocket(s);
if (cbRecv != SOCKET_ERROR)
{
buf[cbRecv] = '\0';
printf("%s", buf);
}
else
{
printf("Error %d on recv.\n", WSAGetLastError());
}
getchar();
return 0;
}
But I thought Hammer said that it will always allow me to write, even when the connection wasn't established yet.
Yes I didn't think of that. My code would need a call to WSAEnumNetworkEvents() to check for errors after the WaitForSingleObject(). I'd suggest you use the code in the thread that hammer posted, as it is simpler anyway. Just replace ioctl() with ioctlsocket() and errno with WSAGetLastError().
EDIT: Replaced code in previous post with working version.
thank you very much.