About gethostname():
I don't see any nonblocking DNS library on CPAN, so I think what you would have to do is create a pipe (or socket pair), fork, do the lookup in the child process and return the result via the pipe (you add the pipe to your select in the parent).
About connect():
Several possibilities:
my $sock = IO::Socket::INET->new(Proto => 'tcp', Blocking => 0) or die "Socket error: $!\n"; my $iaddr = inet_aton($remote) || die "no host: $remote"; my $paddr = sockaddr_in($port, $iaddr); my $ret = connect($sock, $paddr); if (!$ret && ! $!{EINPROGRESS}) { die "Connect error: $!\n"; } # then select(), etc.
use strict; { package NBCSocket; use IO::Socket::INET; use Errno; use Carp; our @ISA = 'IO::Socket::INET'; sub connect { @_ == 2 or croak 'usage: $sock->connect(NAME)'; my $sock = shift; my $addr = shift; my $timeout = ${*$sock}{'io_socket_timeout'}; my $err; my $blocking; $blocking = $sock->blocking(0); if (!connect($sock, $addr)) { if (!$!{EINPROGRESS}) { $err = $!; $@ = "connect: $!"; } } $! = $err if $err; $err ? undef : $sock; } } use IO::Select; use Socket; $| = 1; my $address = shift || die "usage: $0 host:port\n"; $! = 0; my $err; print "Start: ", scalar(localtime), "\n"; my $sock = NBCSocket->new(PeerAddr => $address) or die "Socket error: $!\n"; print "after NBCSocket->new \$! = $!\n"; # of course, you'll probably want to do a select with other sockets $! = 0; my $select = IO::Select->new($sock); if ($select->can_write(5)) { my $peername = $sock->peername; if ($peername) { print "Connected: ", scalar(localtime), ", peername=$peername\n" +; } else { print "Not peername err = $!: ", scalar(localtime), "\n"; } } else { $err = $!; $sock_err = $sock->sockopt(SO_ERROR); print "Did not connect: ", scalar(localtime), "\n"; print "\$!=$err sock_err = $sock_err\n"; }
In reply to Re: Non-blocking connect() and gethostbyname()?
by Thelonius
in thread Non-blocking connect() and gethostbyname()?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |