Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Non blocking connect

by ajwans (Scribe)
on Jul 13, 2004 at 21:48 UTC ( [id://374154]=CUFP: print w/replies, xml ) Need Help??

The perldoc section for getsockopt is outrageously short. Below is the results of my search for a non-blocking connect call.
The key item missing from any documentation that I found was that the return value from 'getsockopt' was an integer packed inside a string. Armed with this I present for your viewing pleasure a code snippet which will perform a non-blocking connect call. Hope some of you out there find it useful. andy
use strict; use warnings; use Fcntl; use Errno; use Socket; use IO::Select; my $socket; my $flags; my $port = 80; my $ip = '1.1.1.1'; # create socket socket($socket, PF_INET, SOCK_STREAM, getprotobyname('tcp')) || die("s +ocket: $!"); # set the socket to non-blocking mode $flags = fcntl($socket, F_GETFL, $flags) || die("fcntl: $!"); fcntl($socket, F_SETFL, $flags | O_NONBLOCK) || die("fcntl: $!"); if (! connect($socket, sockaddr_in($port, inet_aton($ip))) { if ($!{EINPROGRESS}) { ... run a select loop or do something else here installing a callback handler is a good bet ... return; } else { die("connect: $!"); } } else { handle_successful_connect(); } sub callback_handler { my $socket = shift; # check the error condition on the socket my $option = getsockopt($socket, SOL_SOCKET, SO_ERROR); if (! defined($option)) { die("getsockopt: $!"); } if (0 != ($! = unpack('i', $option))) { die("connect: $!"); } handle_successful_connect(); } sub handle_successful_connect { }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-26 07:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found