Solved

Winsock connect() timeout - how to set?

Posted on 1998-10-05
7
1,218 Views
Last Modified: 2008-02-26
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
0
Comment
Question by:nchenkin
7 Comments
 
LVL 6

Expert Comment

by:snoegler
ID: 1253358
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
 
LVL 3

Expert Comment

by:elfie
ID: 1253359
You can use the alarm call, combined with an interrupt handler.
0
 
LVL 4

Accepted Solution

by:
jos010697 earned 100 total points
ID: 1253360
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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

by:nchenkin
ID: 1253361
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
 

Author Comment

by:nchenkin
ID: 1253362
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
 
LVL 4

Expert Comment

by:jos010697
ID: 1253363
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
 

Author Comment

by:nchenkin
ID: 1253364
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

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to find a data that is pure string   or not in vb.net ? 9 103
Problem with form project 66 317
Need example 5 107
delete-remove 14 65
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now