Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Noob help request on TCP Server with fork

Reply
Thread Tools

Noob help request on TCP Server with fork

 
 
xchris
Guest
Posts: n/a
 
      06-12-2004
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);

 
Reply With Quote
 
 
 
 
xchris
Guest
Posts: n/a
 
      06-15-2004
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

 
Reply With Quote
 
 
 
 
Johnny Storm
Guest
Posts: n/a
 
      06-16-2004

"xchris" <> wrote in message
news.. .
> 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


 
Reply With Quote
 
nobull@mail.com
Guest
Posts: n/a
 
      06-18-2004
xchris <> wrote in message news:<> ...
> 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.
 
Reply With Quote
 
xchris
Guest
Posts: n/a
 
      06-21-2004
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
 
Reply With Quote
 
nobull@mail.com
Guest
Posts: n/a
 
      06-22-2004
xchris <> wrote in message news:<> ...
> 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?
 
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
os.fork and pty.fork Eric Snow Python 0 01-08-2009 06:32 AM
Here a noob, there a noob.... JimDoire MCSE 0 04-10-2008 07:23 PM
NAT two outside TCP ports to one inside TCP port Kevin Cisco 1 11-10-2004 08:15 AM
LPR/Standard, TCP/IP, HP TCP/IP Ports DJ Chiro MCSE 1 11-07-2003 08:06 PM
tcp and fork marconi C Programming 4 09-10-2003 03:01 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57