Asynchronous TCP Socket Connect

S

Steve Roscio

Howdy -

Is there a way to do an asynchronous connection of a TCP socket, without
threads or forking? Something like a two-step method, where I start the
connect, then later check to see if it's done and if it worked?

So basically I want to turn this:

my $s = IO::Socket::INET->new(
Proto => $proto,
PeerAddr => $addr,
PeerPort => $port,
# Blocking => 0,
Timeout => $timeout,
);

into a two-step call:
my $s = start_connection (...);

# later...
if ($s->connected() || $s->error()) ... #(whatever)


Thanx!
- Steve
 
M

Martijn Lievaart

Howdy -

Is there a way to do an asynchronous connection of a TCP socket, without
threads or forking? Something like a two-step method, where I start the
connect, then later check to see if it's done and if it worked?

So basically I want to turn this:

my $s = IO::Socket::INET->new(
Proto => $proto,
PeerAddr => $addr,
PeerPort => $port,
# Blocking => 0,
Timeout => $timeout,
);

into a two-step call:
my $s = start_connection (...);

# later...
if ($s->connected() || $s->error()) ... #(whatever)

This is not a Perl question, You are looking for non-blocking sockets.
Whole volumes have been written about this, but best is Stevens "Unix
Network Programming".

HTH,
M4
 
P

Permostat

This is not a Perl question, You are looking for non-blocking sockets.
Whole volumes have been written about this, but best is Stevens "Unix
Network Programming".

HTH,
M4

Yeah Steve. Derp bee derp imma TARD. -> YOU.

squirm-
 
J

Jim Gibson

Martijn Lievaart said:
This is not a Perl question, You are looking for non-blocking sockets.
Whole volumes have been written about this, but best is Stevens "Unix
Network Programming".

But asking how to do it in Perl _is_ a Perl question. Alas, I do not
know the answer. :(

The documentation to IO::Socket::INET indicates that setting 'Blocking
=> 0' in the arguments to IO::Socket::INET make the connect call
non-blocking. The documentation to IO::Socket describes the connected()
method that returns undef if the socket is not in a connected state.
Perhaps those two together, with suitable waiting intervals and
retries, provide the needed functionality.
 
C

C.DeRykus

Howdy -

Is there a way to do an asynchronous connection of a TCP socket, without
threads or forking?  Something like a two-step method, where I start the
connect, then later check to see if it's done and if it worked?

So basically I want to turn this:

     my $s = IO::Socket::INET->new(
         Proto    => $proto,
         PeerAddr => $addr,
         PeerPort => $port,
         #  Blocking => 0,
         Timeout => $timeout,
     );

into a two-step call:
     my $s = start_connection (...);

     # later...
     if ($s->connected() || $s->error()) ...  #(whatever)

Here's a (slightly modified) strategy from
"Network Programming in Perl" (L.Stein):

my $s =IO::Socket::INET->new( ..Blocking=>0 )
...

use Errno qw(EWOULDBLOCK);
...
my $rc = sysread( $s, $data, $bytes );
if ( defined $rc )
{
if ( $rc > 0 ){ # success
...
else { # EOF
...
}
}
elsif ( $! == EWOULDBLOCK )
{
# retry code
}
else
{
# unexpected error .. examine $!
}
 
M

Martijn Lievaart

But asking how to do it in Perl _is_ a Perl question. Alas, I do not
know the answer. :(

If you know the concept, the Perl solution is obvious, as Perl just
exposes the same API. Answering how to do it in perl would be a
disservice, as this is a complex subject, which is best learned from a
book or some other source, not a perl newsgroup.

Rhe short answer is to set all your sockets to non-blocking, then use a
select() loop to determine which handle is ready.

M4
 
P

Peter J. Holzer

If you know the concept, the Perl solution is obvious, as Perl just
exposes the same API.

I disagree. The IO::Socket API (unlike the builtin functions) is
sufficiently abstracted from the system call level that the answer how
to do something is not always obvious.
Rhe short answer is to set all your sockets to non-blocking, then use a
select() loop to determine which handle is ready.

But that wasn't the question. The question was how to set the socket to
non-blocking.

hp
 
S

Steve Roscio

Thanx Uri - You mention the one thing I missed, that should do the trick:
you can tell when a socket is
connected by checking if it can be written

Duh! I missed the obvious. I've got the Stevens book too, somewhere, so
I'll pull that out as a refresher. Thanx all!
- Steve


Uri Guttman, on 03/12/2010 at 04:05 PM:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,730
Latest member
AudryNolan

Latest Threads

Top