How can I prevent perl from exiting during a network error?

Discussion in 'Perl Misc' started by Song Lining, May 8, 2005.

  1. Song Lining

    Song Lining Guest

    Hi, guys

    Got a very frustrating problem while writing a perl socket client to
    connect to a server.

    The perl client will quit by itself everytime I stop the server and
    the errno is either "ECONNRESET: Connection aborted" or "ECONNABORTED:
    Connection reset by peer". The problem is that I need to keep the
    client alive even the server is shutdown, so I tried to ignore all the
    signals in %SIG but that didn't help either. Here's a snipet of the
    code:

    <code>
    ....
    foreach (keys %SIG) {
    print "$_\n";
    $SIG{$_} = 'IGNORE';
    }
    ....
    ....
    $sock = new IO::Socket::INET(
    PeerAddr => $PeerAddr,
    PeerPort => $PeerPort,
    Proto => 'tcp',
    );
    ....
    ....
    </code>

    As you can see, it's a very simple and straitforward code but how can
    it stop exiting??? I think there must be at least one easy way to do
    it :)

    Any clue would be much appreciated!

    Thanks!

    Song Lining
    Song Lining, May 8, 2005
    #1
    1. Advertising

  2. Song Lining

    Anno Siegel Guest

    Song Lining <> wrote in comp.lang.perl.misc:
    > Hi, guys
    >
    > Got a very frustrating problem while writing a perl socket client to
    > connect to a server.
    >
    > The perl client will quit by itself everytime I stop the server and
    > the errno is either "ECONNRESET: Connection aborted" or "ECONNABORTED:
    > Connection reset by peer".


    How do you know that? Is that printed by the code you show?

    > The problem is that I need to keep the
    > client alive even the server is shutdown, so I tried to ignore all the
    > signals in %SIG but that didn't help either. Here's a snipet of the
    > code:
    >
    > <code>
    > ...
    > foreach (keys %SIG) {
    > print "$_\n";
    > $SIG{$_} = 'IGNORE';
    > }
    > ...
    > ...
    > $sock = new IO::Socket::INET(
    > PeerAddr => $PeerAddr,
    > PeerPort => $PeerPort,
    > Proto => 'tcp',
    > );
    > ...
    > ...
    > </code>
    >
    > As you can see, it's a very simple and straitforward code but how can
    > it stop exiting??? I think there must be at least one easy way to do
    > it :)
    >
    > Any clue would be much appreciated!


    From what you show, there is no reason why it shouldn't exit. There's
    no read-loop or anything. It creates the socket and exits, closing the
    socket.

    Show a small program we can run that demonstrates the behavior, and
    explain why you think it should behave otherwise.

    Anno
    Anno Siegel, May 8, 2005
    #2
    1. Advertising

  3. Song Lining

    Song Lining Guest

    Thanks guys!

    Got the problems solved according to your suggestions, here's the
    code:

    do {
    $sock = new IO::Socket::INET(
    PeerAddr => $PeerAddr,
    PeerPort => $PeerPort,
    Proto => 'tcp',
    );
    if (!$sock) {
    print STDERR localtime() . ": Could not connect to M2000, will try
    again in 10 secs.\n";
    select(undef, undef, undef, 10);
    }
    eval {
    while(<$sock>) {
    # do something here...
    }
    }
    # if $! is defined, that means something goes wrong within the eval
    block, normally a network problem.
    } until ($sock && !($!));

    This way, the client will never exit.

    Thanks again!

    Song Lining

    Jim Gibson <> wrote in message news:<090520050921330579%>...
    > In article <>, Song
    > Lining <> wrote:
    >
    > > Hi, guys
    > >
    > > Got a very frustrating problem while writing a perl socket client to
    > > connect to a server.
    > >
    > > The perl client will quit by itself everytime I stop the server and
    > > the errno is either "ECONNRESET: Connection aborted" or "ECONNABORTED:
    > > Connection reset by peer". The problem is that I need to keep the
    > > client alive even the server is shutdown, so I tried to ignore all the
    > > signals in %SIG but that didn't help either. Here's a snipet of the
    > > code:

    >
    > [ client code snipped ]
    >
    > You cannot keep a socket connection alive if the other host involved
    > has closed it. You need to trap and detect this error and repeat the
    > connect step. Implement your client accordingly and keep trying to
    > connect until you either succeed or give up. Put a time delay between
    > connection attempts so that you don't hog the network and inadvertantly
    > create a denial-of-service attack. Intelligent clients will start with
    > a short delay and increase it for each unsuccessful attempt, up to some
    > maximum.
    >
    > Good luck.
    >
    >
    > ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    > http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    > ---= East/West-Coast Server Farms - Total Privacy via Encryption =---
    Song Lining, May 11, 2005
    #3
  4. Song Lining

    Juha Laiho Guest

    (Song Lining) said:
    >Thanks guys!
    >
    >Got the problems solved according to your suggestions, here's the
    >code:

    ....
    > select(undef, undef, undef, 10);


    Out of curiosity, why the above instead of just 'sleep(10)'?
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
    Juha Laiho, May 11, 2005
    #4
    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. Smokey Grindle
    Replies:
    0
    Views:
    689
    Smokey Grindle
    Jul 19, 2006
  2. traveller
    Replies:
    0
    Views:
    1,173
    traveller
    Jan 8, 2008
  3. WISEMANOFNARNIA

    Can I prevent the user from exiting a page?

    WISEMANOFNARNIA, Nov 14, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    303
    George
    Nov 16, 2008
  4. Peter Juuls
    Replies:
    8
    Views:
    185
    Ian Wilson
    Feb 16, 2006
  5. king
    Replies:
    1
    Views:
    265
Loading...

Share This Page