Forking Server

S

SRam

I a writing Forking Server for a Small application..

This is code for multiplexing

my $listen = IO::Socket::INET->new(Proto => 'tcp',LocalPort => 2323,
Listen => 1, Reuse => 1)
or die $!;

$listen->autoflush(1);

my $readable_handles = new IO::Select();
$readable_handles->add($listen);
while (1)
{
$listen->autoflush(1);
($new_readable) = IO::Select->select($readable_handles, undef,
undef, 0);

foreach $sock (@$new_readable)
{

if ($sock == $listen)
{
my $new_sock = $sock->accept();
$readable_handles->add($new_sock);
$count++;
$flag = 0;
print $new_sock->fileno . ": connected\n";
}
else
{
$sock->recv($line,300);
print "Received Message\n";
print "Message = $line\n";
......operations....
}
}
}

I also got code for Forking Server from advanced perl programming

# Forking server
use IO::Socket;
$SIG{CHLD} = sub {wait ()};
$main_sock = new IO::Socket::INET (LocalHost => 'goldengate',
LocalPort => 1200,
Listen => 5,
Proto => 'tcp',
Reuse => 1,
);
die "Socket could not be created. Reason: $!\n" unless ($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";
}
exit(0); # Child process exits when it is done.
} # else 'tis the parent process, which goes back to accept()
}
close ($main_sock);

I need to convert the multipexing one of first to forking..I don't
know exactly where to modify...Can Anyone help me get me the exact
code...
 

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top