My Forking Server works in windows but not linux

Discussion in 'Perl Misc' started by bob Smith, Oct 23, 2003.

  1. bob Smith

    bob Smith Guest

    I stole this example from a book and it works in windows but on Linux it
    shutdowns occasionally after the client connects. Any help would be
    appreciated.
    Linux OP Sys is Redhat ES 3.0:

    Here is the server:

    #!/usr/bin/perl -w
    # Forking server

    use IO::Socket;
    $SIG{CHLD} = sub {wait ()};
    $main_sock = new IO::Socket::INET (LocalHost => '0.0.0.0',
    LocalPort => 5150,
    Listen => 5,
    Proto => 'tcp',
    Reuse => 1,
    );
    die "Socket could not be created. Reason: $!\n" unless ($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";
    }
    $new_sock->shutdown(0);
    $new_sock->send("000065TCP FEDI
    HELLO");
    $new_sock->shutdown(2);
    exit(0); # Child process exits when it is done.
    } # else 'tis the parent process, which goes back to accept()
    }
    close ($main_sock);

    Here is the client:

    #!/usr/bin/perl -w
    use IO::Socket;
    $sock = new IO::Socket::INET (PeerAddr => 'localhost',
    PeerPort => 5150,
    Proto => 'tcp'
    );
    die "Socket could not be created. Reason: $!\n" unless $sock;
    foreach (1 .. 10) {
    print "Hello $_: \n";
    print $sock "Msg $_: How are you?\n";
    #$sock->flush();
    }
    $sock->shutdown(1);
    $sock->flush();
    my $line;
    while (defined($line = <$sock>)) {
    print $line;
    }
    close ($sock);
     
    bob Smith, Oct 23, 2003
    #1
    1. Advertisements

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. Ed
    Replies:
    0
    Views:
    797
  2. Martin Holmgaard
    Replies:
    4
    Views:
    563
    Martin Holmgaard
    Feb 17, 2005
  3. Ulrich Eckhardt

    Printing works on windows but not on linux

    Ulrich Eckhardt, Jun 29, 2006, in forum: Java
    Replies:
    1
    Views:
    636
  4. Christian Seberino
    Replies:
    2
    Views:
    653
    Christian Seberino
    Feb 12, 2004
  5. John fabiani
    Replies:
    14
    Views:
    681
    Donn Cave
    Jul 2, 2004
  6. F. GEIGER
    Replies:
    3
    Views:
    1,061
    F. GEIGER
    Aug 6, 2004
  7. D. Buck
    Replies:
    2
    Views:
    973
    D. Buck
    Jun 29, 2004
  8. ian douglas
    Replies:
    5
    Views:
    301
    88888 Dihedral
    Mar 5, 2013
Loading...