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. 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. Ulrich Eckhardt

    Printing works on windows but not on linux

    Ulrich Eckhardt, Jun 29, 2006, in forum: Java
    Replies:
    1
    Views:
    506
  2. Christian Seberino
    Replies:
    2
    Views:
    435
    Christian Seberino
    Feb 12, 2004
  3. John fabiani
    Replies:
    14
    Views:
    508
    Donn Cave
    Jul 2, 2004
  4. ian douglas
    Replies:
    5
    Views:
    181
    88888 Dihedral
    Mar 5, 2013
  5. Wolfgang Strobl
    Replies:
    0
    Views:
    137
    Wolfgang Strobl
    Mar 4, 2013
Loading...

Share This Page