nchenkin
asked on
Winsock connect() timeout - how to set?
Does anyone know how to set the timeout when doing a Windows Socket connect()?
I can set the timeout on send() or recv() by calling select() first, but you need to be connected before select() works.
So how to set the timeout when trying to connect()?
Thanks,
NC
I can set the timeout on send() or recv() by calling select() first, but you need to be connected before select() works.
So how to set the timeout when trying to connect()?
Thanks,
NC
You can use the alarm call, combined with an interrupt handler.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
snoegler and elfie,
Thanks for your comments. I had forgot to mention that I was using blocking sockets, but jos realized that. I think his answer is what I was looking for.
jos,
That looks like a good solution. I'll test it out today or tomorrow, but the points are yours for now!
Thanks,
Nelson
Thanks for your comments. I had forgot to mention that I was using blocking sockets, but jos realized that. I think his answer is what I was looking for.
jos,
That looks like a good solution. I'll test it out today or tomorrow, but the points are yours for now!
Thanks,
Nelson
ASKER
jos,
Actually, I have one more question if you don't mind. After doing the initial connect, then can you set the socket back in blocking? So the sequence is:
Set to non-blocking with ioctl()
Connect
Set to blocking with iotcl()
Select with timeout
Communicate with client or handle timeout error...
Does that look OK?
Thanks,
NC
Actually, I have one more question if you don't mind. After doing the initial connect, then can you set the socket back in blocking? So the sequence is:
Set to non-blocking with ioctl()
Connect
Set to blocking with iotcl()
Select with timeout
Communicate with client or handle timeout error...
Does that look OK?
Thanks,
NC
No, set the socket back to blocking mode _after_ the select has returned
succesfully (you're sure the connect has completed then). I really don't
have any idea what would happen if you'd do it the way you described ...
kind regards,
Jos aka jos@and.nl
succesfully (you're sure the connect has completed then). I really don't
have any idea what would happen if you'd do it the way you described ...
kind regards,
Jos aka jos@and.nl
ASKER
jos,
Thanks for your now ancient response. The day it arrived I was on a plane heading off for 5 weeks of trekking in the Himalayas. Just back in the office today and wanted to acknowledge your resonse.
Actually, I think I implemented it the way I mentioned above and it <seems> to be working, but will probably change to your suggestion.
NC
Thanks for your now ancient response. The day it arrived I was on a plane heading off for 5 weeks of trekking in the Himalayas. Just back in the office today and wanted to acknowledge your resonse.
Actually, I think I implemented it the way I mentioned above and it <seems> to be working, but will probably change to your suggestion.
NC
Perhaps you could create a thread which calls connect(), and then timeout this thread:
(pseudocode):
int mythread()
{
return connect();
}
myfunc()
{
.
BeginThread(mythread);
if(WaitForSingleObject(myt
TerminateThread(mythread);
// timeout ...
} else
{
SOCKET sock=GetResultCode(mythrea
}
}