Under win32 ActivePerl, I'm using the following code, and when i make a connection to port 8009 it works fine, but when i make connections to port 8008, right when i try to connect a second time, the program crashes. code:
$serverA (8008) is the one that forks, and the one that crashes. $serverL (8009) works fine.
Oddly enough, the program crashes on the line with three stars before it.
use IO::Socket;
use IO::Select;

#require 'c:\perl\banner.pl';

sub spawn; #forward declaration

# Create a socket to listen on.
#
my $serverA = #handles ad requests
  IO::Socket::INET->new( LocalPort => 8008, Listen => 5, Reuse => 1 );
my $serverL = #handles log requests
  IO::Socket::INET->new( LocalPort => 8009, Listen => 5, Reuse => 1 );

die "Can't create socket for listening: $!" unless $serverA;
print "Listening for connections on port 8008\n";
die "Can't create socket for listening: $!" unless $serverL;
print "Listening for connections on port 8009\n";


my $readable = IO::Select->new;     # Create a new IO::Select object
$readable->add($serverA);           # Add the servers to it
$readable->add($serverL);

while(1) {

    # Get a list of sockets that are ready to talk to us.
    #
    my ($ready) = IO::Select->select($readable, undef, undef, undef);
#***
    foreach my $s (@$ready) {
        # Is it a new connection?
        #
        if($s == $serverL) {
        
            # Grab the request, close the socket, and log it as fast as we can
            #
            my $new_sock = $serverL->accept;
            my $in=<$new_sock>;
            $new_sock->close;
#            slog($in);
            print $in;

            
        } elsif($s == $serverA) {
            # Fork off, get the request, and reply with the banner
            #
            my $new_sock = $serverA->accept;
            spawn $new_sock;

            
        } else {  # It's an established connection, and shouldn't be here
            $s->close;
        }
    }
}

sub spawn {
    my $client=shift;
    my $pid;

    if (!defined($pid = fork)) {
        die "can't fork";
        return;
    } elsif ($pid) {
        return; # I'm the parent
    }
    $in=<$client>; #get the request
#    print $client &process_request($in);
    print $client $in;
    $client->close;
    exit;
}


Thanks,    --tom

In reply to Problems with fork or socket code??? by mortenal

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.