Socket problem in C++

Socket problem in C++

Post by Harbinger » Thu, 10 Jul 2003 03:36:53


Hello,

I'm currently working on a C++ project that needs to send data over a
network
I use the SOCKET datatype to do that.
Everything works perfectly, but there's just one problem:
If I try to connect to a server that isn't listening, the connect() function
hangs.
I've been looking for something to set a time-out for the connection, but I
can't seem to find anything.
Is there some way to set a connection time-out? and if so, how?
thanks in advance
 
 
 

Socket problem in C++

Post by mik0 » Fri, 11 Jul 2003 01:44:41


I don't know an ability to set an timeout for the connect function...
Since there are many functions in the sockets api, which don't return
immediately I would recommend to put all socket functionality in an
separated thread.

--
To reply by eMail remove the donald from the adress.

 
 
 

Socket problem in C++

Post by Dave C » Fri, 11 Jul 2003 04:15:48


Win32 doesn't support the SO_SNDTIMEO or SO_RCVTIMEO options. You might
try using the writefds descriptor in the select() method - I believe that
checks for the ability to connect on nonblocking sockets, not so sure
about blocking, and allows a timeout value. So you'd call select() right
before trying connect(). If that doesn't work, I think you might have to
use either the nonblocking sockets, or spawn off some kind of watchdog
thread which will call closesocket() (or maybe shutdown()) on the socket
if your main thread doesn't indicate that it was able to connect within a
specified amount of time (Calling closesocket from another thread on the
socket should cause the main thread's call to connect() to
return immediately with an error code). That latter alternative is a bit
hairy but I've used a similar method to quickly terminate some threads
when a user presses 'X' in a GUI.

The win32 docs on select(), shutdown(), closesocket(), etc might help
more.

Dave

--
Dave Chisholm
www.60md.com - My band
www.davechisholm.net - My instrumental guitar album
 
 
 

Socket problem in C++

Post by Harbinger » Fri, 11 Jul 2003 07:31:54


"Dave C." < XXXX@XXXXX.COM > schreef in bericht




Thanks a lot dave,
The two threads sound like a good solution,
since no one in any group knows about a
possibility to set a connection time-out.
someone ought to write something for that!
(my skills don't reach that far yet, unfortunately)
 
 
 

Socket problem in C++

Post by Harbinger » Fri, 11 Jul 2003 07:35:08


"Dave C." < XXXX@XXXXX.COM > schreef in bericht




Thanks a lot dave,
The two threads sound like a good solution,
since no one in any group knows about a
possibility to set a connection time-out.
someone ought to write something for that!
(my skills don't reach that far yet, unfortunately)
 
 
 

Socket problem in C++

Post by Andy Walld » Fri, 11 Jul 2003 20:32:37


You need to set the socket to non-blocking, issue the connect, and if
you get an error of WSAEWOULDBLOCK, you can wait for a write status with
select. You can specify a timeout with select.
 
 
 

Socket problem in C++

Post by Harbinger » Fri, 11 Jul 2003 21:48:05


"Andy Walldorff" < XXXX@XXXXX.COM > schreef in bericht


function
but I

Andy, If you weren't a guy, I would kiss you! ;-)
this is exactly what I was looking for!!
thank you very much!!
 
 
 

Socket problem in C++

Post by anaki » Sat, 12 Jul 2003 00:55:33

> Win32 doesn't support the SO_SNDTIMEO or SO_RCVTIMEO options.

This is not true. I use both all the time and they work fine.

setsockopt, just does it.
 
 
 

Socket problem in C++

Post by Dave C » Sat, 12 Jul 2003 01:31:14


Hmm, the docs are confusing...

The win32 docs I have installed locally definitely say those are
not supported, but the following (probably more up to date) link is
ambigious:

http://www.yqcomputer.com/

If you scroll down you'll see:



BSD options not supported for setsockopt are shown in the following table.

Value Type Meaning
SO_ACCEPTCONN BOOL Socket is listening.
SO_RCVLOWAT int Receives low watermark.
SO_RCVTIMEO int Receives time-out in milliseconds (available in the
Microsoft implementation of Windows Sockets 2).
SO_SNDLOWAT int Sends low watermark.
SO_SNDTIMEO int Sends time-out in milliseconds (available in the Microsoft
implementation of Windows Sockets 2).
SO_TYPE int Type of the socket.



I don't know what an option that is "not supported" but also "available"
actually is... perhaps this is a winsock vs winsock2 thing, or varies on
different versions of windows? ANyone know the correct explanation of
those docs?

Dave
 
 
 

Socket problem in C++

Post by Dave C » Sat, 12 Jul 2003 01:38:23


Further reading indicates it is a winsock1.1 vs winsock2 issue. Oh well.

Dave

--
Dave Chisholm
www.60md.com - My band
www.davechisholm.net - My instrumental guitar album