![]() |
Noob help request on TCP Server with fork
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); |
Re: Noob help request on TCP Server with fork
On Sat, 12 Jun 2004 15:14:50 +0200, xchris wrote:
> 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 |
Re: Noob help request on TCP Server with fork
"xchris" <lyralyraSP@Mfastmail.fm> wrote in message news:pan.2004.06.15.09.21.18.379092@Mfastmail.fm.. . > On Sat, 12 Jun 2004 15:14:50 +0200, xchris wrote: > > > 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 > 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 |
Re: Noob help request on TCP Server with fork
xchris <lyralyraSP@Mfastmail.fm> wrote in message news:<pan.2004.06.12.13.14.50.560101@Mfastmail.fm> ...
> 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. |
Re: Noob help request on TCP Server with fork
On Fri, 18 Jun 2004 05:40:42 -0700, nobull wrote:
> > 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. > >> 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. 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 |
Re: Noob help request on TCP Server with fork
xchris <lyralyraSP@Mfastmail.fm> wrote in message news:<pan.2004.06.21.07.09.55.393918@Mfastmail.fm> ...
> On Fri, 18 Jun 2004 05:40:42 -0700, nobull wrote: > > > > 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. 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? |
| All times are GMT. The time now is 09:22 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.