Noob help request on TCP Server with fork

X

xchris

I'm really new to perl. (2 days)

i'm getting crazy for a simple server that should fork for each connection
...say "Hi Mon" and then close connection.

i paste my code.Can you help me? 10x a lot
i took part of the code from a book and the net.
bye :)

xchris


use IO::Socket;
use POSIX qw:)sys_wait_h);

sub REAPER {
1 until (-1 == waitpid(-1,WNOHANG));
$SIG{CHLD} = \&REAPER;
}


socket(SERVER,PF_INET,SOCK_STREAM,getprotobyname('tcp'));
setsockopt(SERVER,SOL_SOCKET,SO_REUSADDR,1);
$my_addr=sockaddr_in("9999",INADDR_ANY);
bind (SERVER,$my_addr)
or die "can't bind";
listen (SERVER,SOMAXCONN)
or die "Cant listen";

$SIG{CHLD}= \&REAPER;

print ("Listening\n");
while ($client_addr = accept(CLIENT,SERVER)) {
## ok new connection... let's fork
$pid = fork;
die "fork: $!" unless defined $pid;
print "Pid: $pid\n";
if ($pid == 0 )
{
## we're inside child
print CLIENT "Hi Mon!\n";
exit;
}
}
close (SERVER);
 
X

xchris

I'm really new to perl. (2 days)

i'm getting crazy for a simple server that should fork for each connection
..say "Hi Mon" and then close connection.

i paste my code.Can you help me? 10x a lot

nobody knows?
10x

xchris
 
J

Johnny Storm

xchris said:
nobody knows?
10x

xchris

I don't have perl installed on any system that i can access right now, so I
can only guess right now, but I have a pretty good guess.
I tried something similar a while back, trying to write a mulit-threaded
perl server, only to discover than when you fork (or rather, when I did it),
it only forked about 4 connections, and then refused to close the
connections when done. I don't know if it's something I did or something
perl just naturally does automatically.
Your best bet is to use google and use the words 'threading perl' to help
you get some serious answers.


Johnny
 
N

nobull

xchris said:
I'm really new to perl. (2 days)

In that case get into good habits now. Put "use strict;" and "use
warnings;" at the top of your programs. Using strict disables 3
features that you probably don't won't use yet and you certainly don't
want Perl accidently interpreting a mistake as an attempt to use on of
these features.

Declare all variables as lexically scoped (using my) in the smallest
applicable scope unless you have a reson to do otherwise.

Include the actual error in error mesages.

These things are like using ropes when climbing. They may seem to
slow you down but actually after a a few slips you'll find that you
are making better time with the ropes than you would without. Use
ropes from the outset and soon they'll become instinctive. Learn to
free climb first and (if you don't kill yourself) you'll find that
trying to go back and learn how to use ropes feels really cumbersome.
i'm getting crazy for a simple server that should fork for each connection
..say "Hi Mon" and then close connection.

What does it actually do on your computer? (Is your computer one with
a OS that implements fork?)

I cut and pasted it onto my machine and it worked fine without any
changes.
i took part of the code from a book and the net.

Note: you are using the old "Socket" interface rather than the new
"IO::Socket" interface (although, perversely, you are telling Perl to
load the new interface).

If the book teaches the old interface as the socket interface in Perl
and doesn't tell you about wanrings and strict then you probably
should replace it.

This newsgroup does not exist (see FAQ). Please do not start threads
here.
 
X

xchris

What does it actually do on your computer? (Is your computer one with
a OS that implements fork?)

yes! using linux

I cut and pasted it onto my machine and it worked fine without any
changes.

it seems to work but if a "ps -e" from another shell i see that new
process doesn't die if i close connection from client.
Note: you are using the old "Socket" interface rather than the new
"IO::Socket" interface (although, perversely, you are telling Perl to
load the new interface).

If the book teaches the old interface as the socket interface in Perl
and doesn't tell you about wanrings and strict then you probably
should replace it.

it is a cut'n paste from book,net... so i immagine it's not that clean
This newsgroup does not exist (see FAQ). Please do not start threads
here.

Ok! :)
Thank you.

xchris
 
N

nobull

xchris said:
yes! using linux


it seems to work but if a "ps -e" from another shell i see that new
process doesn't die if i close connection from client.

Not on my machine with perl5.6.1 or 5.8.0 and Linux kernel 2.4.18.

What Perl version and kernel version are you using?
 

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,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top