Noob help request on TCP Server with fork

Discussion in 'Perl' started by xchris, Jun 12, 2004.

  1. xchris

    xchris Guest

    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);
     
    xchris, Jun 12, 2004
    #1
    1. Advertising

  2. xchris

    xchris Guest

    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
     
    xchris, Jun 15, 2004
    #2
    1. Advertising

  3. xchris

    Johnny Storm Guest

    "xchris" <> wrote in message
    news:p...
    > 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
     
    Johnny Storm, Jun 16, 2004
    #3
  4. xchris

    Guest

    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.
     
    , Jun 18, 2004
    #4
  5. xchris

    xchris Guest

    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
     
    xchris, Jun 21, 2004
    #5
  6. xchris

    Guest

    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?
     
    , Jun 22, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. marconi

    tcp and fork

    marconi, Sep 9, 2003, in forum: C Programming
    Replies:
    4
    Views:
    552
    Keith Thompson
    Sep 10, 2003
  2. Tiger
    Replies:
    5
    Views:
    974
    Dave Thompson
    May 1, 2006
  3. Replies:
    8
    Views:
    393
    Oliver Wong
    Mar 21, 2007
  4. Replies:
    1
    Views:
    250
    Jon Clements
    Sep 9, 2008
  5. Eric Snow

    os.fork and pty.fork

    Eric Snow, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    574
    Eric Snow
    Jan 8, 2009
Loading...

Share This Page