My Forking Server works in windows but not linux

B

bob Smith

I stole this example from a book and it works in windows but on Linux it
shutdowns occasionally after the client connects. Any help would be
appreciated.
Linux OP Sys is Redhat ES 3.0:

Here is the server:

#!/usr/bin/perl -w
# Forking server

use IO::Socket;
$SIG{CHLD} = sub {wait ()};
$main_sock = new IO::Socket::INET (LocalHost => '0.0.0.0',
LocalPort => 5150,
Listen => 5,
Proto => 'tcp',
Reuse => 1,
);
die "Socket could not be created. Reason: $!\n" unless ($main_sock);
while ($new_sock = $main_sock->accept()) {
$pid = fork();
die "Cannot fork: $!" unless defined($pid);
if ($pid == 0) {
# Child process
while (defined ($buf = <$new_sock>)) {
# do something with $buf ....
print $new_sock "You said: $buf\n";
}
$new_sock->shutdown(0);
$new_sock->send("000065TCP FEDI
HELLO");
$new_sock->shutdown(2);
exit(0); # Child process exits when it is done.
} # else 'tis the parent process, which goes back to accept()
}
close ($main_sock);

Here is the client:

#!/usr/bin/perl -w
use IO::Socket;
$sock = new IO::Socket::INET (PeerAddr => 'localhost',
PeerPort => 5150,
Proto => 'tcp'
);
die "Socket could not be created. Reason: $!\n" unless $sock;
foreach (1 .. 10) {
print "Hello $_: \n";
print $sock "Msg $_: How are you?\n";
#$sock->flush();
}
$sock->shutdown(1);
$sock->flush();
my $line;
while (defined($line = <$sock>)) {
print $line;
}
close ($sock);
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top