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

S

Song Lining

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
 
A

Anno Siegel

Song Lining said:
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
 
S

Song Lining

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 said:
Song said:
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.
 
J

Juha Laiho

(e-mail address removed) (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)'?
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top