I've been working on a very simple server to learn how things work and I'm having one major problem that I can't get past. If I run more that one client at a time and either client is killed then the server won't accept any more connections until all clients are killed.

For example: client 1 connects, client 2 connects, client 2 is killed, client 3 tries to connect but hangs until client 1 is killed.

The server code is below:
#!/usr/bin/perl use IO::Socket; use Sys::Hostname; use POSIX qw(:sys_wait_h); #testserver $version=".00"; $debug=0; $myip='192.168.0.2'; $true=1; #---------------------------------- BetQ.d Main ---------------------- +-------------------- &do_socket; #---------------------------------- BetQ.d Main ---------------------- +-------------------- #---------------------------------- Subroutines ---------------------- +-------------------- sub S_print { print $new_sock (@_); } sub do_socket { $SIG{CHLD} = \&REAP; $sock = new IO::Socket::INET( LocalHost => $myip, LocalPort => 8721, Proto => 'tcp', Listen => SOMAXCONN, Reuse => 1); $sock or die "no socket :$!"; STDOUT->autoflush(1); while (($new_sock, $c_addr) = $sock->accept()) { ($client_port, $c_ip) = sockaddr_in($c_addr); $client_ipnum = inet_ntoa($c_ip); $client_host = gethostbyaddr($c_ip, AF_INET); # execute a fork, if this is # the parent, its work is done, # go straight to continue $i=0; $done=0; $betsend=0; print "[$client_ipnum] $client_host connected\n"; next if $kid = fork; die "fork: $!" unless defined $kid; # child now... # close the server - not needed #print "closing sock\n"; close $sock; #print "Forked child printing CMDSTART to client\n"; $date=`date`; print $new_sock "[$client_ipnum] $client_host $date co +nnection attempt logged\n"; print $new_sock "This is a private server.\n"; print $new_sock "username:\n"; if (defined($lineread = <$new_sock>)) { print "un received: $lineread"; print $new_sock "$lineread"; print $new_sock "password:\n"; } if (defined($lineread = <$new_sock>)) { print "pw received: $lineread"; print $new_sock "$lineread"; } if ($lineread eq "start\n"){ print $new_sock "Accepted\n"; $first=1; &Send_Bets; }else{ sleep(5); print $new_sock "Denied and logged\n"; } print "lineread is -->$lineread<--\n"; print "Breaking connection to [$client_ipnum] $client_ +host\n"; exit; } continue { # parent closes the client since # it is not needed print "closing new sock\n"; close $new_sock; } print "IN 7\n"; } sub REAP { print "reaping\n"; 1 until (-1 == waitpid(-1, WNOHANG)); $SIG{CHLD} = \&REAP; } sub Send_Bets { $done=0; while(!$done){ if ($first || defined($lineread = <$new_sock>)) { print "startcmd is $lineread"; $first=0; for ($f=0;$f<=10;$f++) { print "sending: $f"; &S_print (" sent $f\n"); } print "sending: EOF\n"; &S_print ("EOF\n"); } if ($lineread ne "start\n"){ $done=$true; } } }

edited: Mon Nov 18 15:30:18 2002 by jeffa - s/pre/code/g


In reply to Socket question Help! by drake50

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.