• C

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
nchenkinAsked:
Who is Participating?
 
jos010697Connect With a Mentor Commented:
The funny thing is that one can set read/write timeout values, but no connect
timeout values for a socket. There's a way out though:

-- set the socket to nonblocking:

   int noblock= 1;
   ioctl(socket, FIONBIO, &noblock);

- connect the socket:

   struct sockaddr_in sin;
   int retval= connect(socket,  &sin sizeof(sin));

- if the connect succeeds (immediately, that is), nothing needs to be done;
  otherwise, if connect returned -1 and errno is set to EINPROGRESS,
  the socket is still busy connecting. A select() call can be performed
  (check if it is ready for writing).

The select call _does_ have a user definable timeout value (it's the last parameter)
and voila, there you've got what you want.

kind regards,

Jos aka jos@and.nl

0
 
snoeglerCommented:
Just a thought:
Perhaps you could create a thread which calls connect(), and then timeout this thread:
(pseudocode):

int mythread()
{
  return connect();
}

myfunc()
{
.
  BeginThread(mythread);
  if(WaitForSingleObject(mythread, TIMEOUT)!=WAIT_OBJECT_0) {
    TerminateThread(mythread);
    // timeout ...
  } else
  {
    SOCKET sock=GetResultCode(mythread);
  }
}
0
 
elfieCommented:
You can use the alarm call, combined with an interrupt handler.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
nchenkinAuthor Commented:
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


0
 
nchenkinAuthor Commented:
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
0
 
jos010697Commented:
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
0
 
nchenkinAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.