Asynchronous TCP Socket Connect

Discussion in 'Perl Misc' started by Steve Roscio, Mar 12, 2010.

  1. Steve Roscio

    Steve Roscio Guest

    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
    Steve Roscio, Mar 12, 2010
    #1
    1. Advertising

  2. On Fri, 12 Mar 2010 10:10:25 -0700, Steve Roscio wrote:

    > 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
    Martijn Lievaart, Mar 12, 2010
    #2
    1. Advertising

  3. Steve Roscio

    Permostat Guest

    On Mar 12, 12:23 pm, Martijn Lievaart <> wrote:
    > On Fri, 12 Mar 2010 10:10:25 -0700, Steve Roscio wrote:
    > > 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


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

    squirm-
    Permostat, Mar 12, 2010
    #3
  4. Steve Roscio

    Jim Gibson Guest

    In article <>, Martijn Lievaart
    <> wrote:

    > On Fri, 12 Mar 2010 10:10:25 -0700, Steve Roscio wrote:
    >
    > > 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".


    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.

    --
    Jim Gibson
    Jim Gibson, Mar 12, 2010
    #4
  5. Steve Roscio

    C.DeRykus Guest

    On Mar 12, 9:10 am, Steve Roscio <> wrote:
    > 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 $!
    }

    --
    Charles DeRykus
    C.DeRykus, Mar 13, 2010
    #5
  6. On Fri, 12 Mar 2010 14:39:10 -0800, Jim Gibson wrote:

    >>
    >> 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. :(


    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
    Martijn Lievaart, Mar 13, 2010
    #6
  7. On 2010-03-13 13:09, Martijn Lievaart <> wrote:
    > On Fri, 12 Mar 2010 14:39:10 -0800, Jim Gibson wrote:
    >>> 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. :(

    >
    > 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
    Peter J. Holzer, Mar 15, 2010
    #7
  8. Steve Roscio

    Steve Roscio Guest

    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:
    >>>>>> "JG" == Jim Gibson <> writes:

    >
    > JG> In article <>, Martijn Lievaart
    > JG> <> wrote:
    >
    > >> On Fri, 12 Mar 2010 10:10:25 -0700, Steve Roscio wrote:
    > >>
    > >> > 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".

    >
    > JG> But asking how to do it in Perl _is_ a Perl question. Alas, I do not
    > JG> know the answer. :(
    >
    > JG> The documentation to IO::Socket::INET indicates that setting 'Blocking
    > JG> => 0' in the arguments to IO::Socket::INET make the connect call
    > JG> non-blocking. The documentation to IO::Socket describes the connected()
    > JG> method that returns undef if the socket is not in a connected state.
    > JG> Perhaps those two together, with suitable waiting intervals and
    > JG> retries, provide the needed functionality.
    >
    > even better is to use an event loop and you can tell when a socket is
    > connected by checking if it can be written. this is how event loops do
    > async socket connections while also handling other events. you do need
    > to set the socket to non-blocking before the connect call. and this will
    > not affect the block nature of the i/o so if you want async i/o too you
    > need to set non-blocking AFTER the connect happens.
    >
    > uri
    >
    Steve Roscio, Mar 18, 2010
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tor Erik
    Replies:
    12
    Views:
    8,812
    Rhamphoryncus
    Sep 5, 2006
  2. Tiger
    Replies:
    5
    Views:
    951
    Dave Thompson
    May 1, 2006
  3. Laszlo Nagy
    Replies:
    1
    Views:
    4,755
    Mark Wooding
    Jan 27, 2009
  4. Jean-Paul Calderone
    Replies:
    0
    Views:
    942
    Jean-Paul Calderone
    Jan 27, 2009
  5. Laszlo Nagy
    Replies:
    0
    Views:
    524
    Laszlo Nagy
    Feb 1, 2009
Loading...

Share This Page