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
{
}