Ask a Question related to PERL Miscellaneous, Design and Development.
-
SRam #1
Forking Server
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...
SRam Guest
-
Net::Telnet and forking multiple processes
Under normal conditions the entire program works, however when trying to fork multiple processes, the script cannot open multiple telnet sessions... -
Forking for performance?
Ok, first off I should preface this by saying I've never forked before. Guess you could call me a virgin :) Anyways, I'm used to writing scripts... -
forking and ending a CGI process
Hi; I spent about 4 hours searching through newsgroups, faqs, and O'reilly books with various odd-smelling desert animals on the cover. I found... -
Pre-forking SOAP servers
Hi all, Rob Riepel, in http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=mvyitg0vhui.fsf%40Zathras.Stanford.EDU&rnum=2 indicates... -
Perl Forking Server Problems
littleretsam@yahoo.com (Jeff Chinnock) wrote in message news:<49fcf7a7.0306251728.1a1a4160@posting.google.com>... This looks like an infinite... -
Robert Fonda #2
Re: Forking Server
This looks allot like a homework assignment.
"SRam" <aksivaram2k2@yahoo.com> wrote in message
news:20ffa179.0309142351.1dee07c8@posting.google.c om...> 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...
Robert Fonda Guest
-
Jim Gibson #3
Re: Forking Server
SRam wrote:
Nobody else has replied constructively, so I will attempt to. I have not> I a writing Forking Server for a Small application..
used the IO::Socket module, but have done some server programming in C
and am familiar with some of the issues.You probably want to increase Listen to more than 1. The default is>
> This is code for multiplexing
>
> my $listen = IO::Socket::INET->new(Proto => 'tcp',LocalPort => 2323,
> Listen => 1, Reuse => 1)
> or die $!;
usually 5. If you leave it at only 1, you are liable to miss a client
when two or more try to connect at the same time. This number is the
number of waiting clients that will be queued up when trying to connect
to your socket.
This is probably redundant to the autoflush call above. Once set, this>
> $listen->autoflush(1);
>
> my $readable_handles = new IO::Select();
> $readable_handles->add($listen);
> while (1)
> {
> $listen->autoflush(1);
switch shouldn't change.
You now have a handle to a socket object for a newly-connected client.> ($new_readable) = IO::Select->select($readable_handles, undef,
> undef, 0);
>
> foreach $sock (@$new_readable)
> {
>
> if ($sock == $listen)
> {
> my $new_sock = $sock->accept();
If you want to fork off a process to serve this client, this is the
place to do it. In that case, you probably don't want to add this client
to the set of sockets being waited on by the select statement, and your
select statement will never be looking at more than one socket: the
original server socket.
You now have a handle to an IO::Socket::INET object that has data ready> $readable_handles->add($new_sock);
> $count++;
> $flag = 0;
> print $new_sock->fileno . ": connected\n";
> }
> else
> {
for reading. If you want to fork off a process to process the data, this
is the place to do it.
You haven't explained why you want to do this. In general, a server> $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...
would be implemented either by multiplexing or multiprocessing
(forking). Multiplexing using select lets you get data from a client
without blocking because you are told when the data is ready. The
advantage is simplicity (once you figure out how the select statement
works), efficiency (because you only have one process), and
responsiveness (because you don't have to wait for a process to fork).
The disadvantage is unresponsiveness if it takes a long time to service
a client, and other clients are waiting. Advanced Perl Programming, by
Srinivasan, discusses both of these approaches.
So why do you want to do both multiplexing and multiprocessing?
Jim Gibson Guest



Reply With Quote

