Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Setting timeout threshold when connecting to a socket.

by ehdonhon (Curate)
on Dec 14, 2001 at 00:09 UTC ( [id://131739]=perlquestion: print w/replies, xml ) Need Help??

ehdonhon has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have a bit of legacy code that I want to avoid changing as much as possible:

my $iaddr = &inet_aton("$sname") || return 5; socket(SH,PF_INET,SOCK_STREAM,getprotobyname('tcp')); connect(SH,sockaddr_in($port,$iaddr)) || return 6;

The issue is that I want to modify the amount of time that I am spending waiting for the connect before I timeout. I think that I can use setsockopt to do this. Reading the unix setsockopt (2) manpage (Using FreeBSD 4.1.1-STABLE), it appears that there are two variables associated with timeouts:

           SO_SNDTIMEO        set timeout value for output
           SO_RCVTIMEO        set timeout value for input

My Questions:

  1. Do either of these affect the timeout for a connect or do they only apply to connected sockets?
  2. If not, what is the correct way to set the connect timeout (if it can be done)?
  3. According to man setsockopt, these options require a timeval struct in c to work, what is the correct data structure to modify these options with the perl setsockopt?

Note: Yes, I know, I could do all this so much easier with IO::Socket, but let's just assume that is not an option here.

Thanks!

Replies are listed 'Best First'.
Re: Setting timeout threhold when connecting to a socket.
by gbarr (Monk) on Dec 14, 2001 at 01:39 UTC
    IIRC, SO_SNDTIMEO and SO_RCVTIMEO do not apply to the connect.

    If you do not want to use IO::Socket then your best bet is to do what it does. First place the socket into non-blocking mode, then call connect, then use select(). The socket will become readable when connected.

Re: Setting timeout threhold when connecting to a socket.
by Zaxo (Archbishop) on Dec 14, 2001 at 00:33 UTC

    perldoc -f setsockopt:

    setsockopt SOCKET,LEVEL,OPTNAME,OPTVAL
    Sets the socket option requested. Returns undefined if there is an error. OPTVAL may be specified as "undef" if you don't want to pass an argument.
    Update: use Socket; contains the symbols for the options. That will save autoloating inet_aton() too

    After Compline,
    Zaxo

      Thanks, but I've alread read that (in fact, I even created a link to that in my original message). Either I am missing something really obvious, or the perldoc isn't al that helpful here

      I guess my questions are: what is the correct OPTNAME to control the connection timeout, and how do I do I know what kind of data structure to use in OPTVAL?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://131739]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2025-06-16 18:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.