Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Forking server

Reply
Thread Tools

Forking server

 
 
SRam
Guest
Posts: n/a
 
      08-21-2003
How to create a threaded process in Perl. I need explanation for this
code 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);
 
Reply With Quote
 
 
 
 
dw
Guest
Posts: n/a
 
      08-21-2003

"SRam" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> How to create a threaded process in Perl. I need explanation for this
> code from Advanced Perl Programming....


First of all... you probably should take this over to comp.lang.perl.misc.
People complain that this group doesn't exist, you you may get better
answers over there....

>
> # 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);


I'm guessing that $sock should be $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";
> }
> exit(0); # Child process exits when it is done.
> } # else 'tis the parent process, which goes back to accept()
> }
> close ($main_sock);


Advanced Perl Programming has a short description after the segment of code.
You didn't say what you don't understand: the code, the way a server works,
why the code always died? If you say what you don't understand... maybe you
can get a better answer.





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Daemon Server, Forking, Defunct Processes Scottman C Programming 2 09-11-2008 03:11 PM
(pre)forking server framework? czajnik@czajsoft.pl Python 3 06-12-2006 02:49 PM
forking socket server under Windows Ed Perl 0 04-19-2004 07:43 PM
Forking Server SRam Perl Misc 0 09-15-2003 07:51 AM
Forking Server SRam Perl 0 09-15-2003 07:47 AM



Advertisments