I have cobbled together a primitive server, directly from the cookbook. It works, as long as only one client is connected. Being greedy, I would to have the program accept more than one connection.

"Ah-ha!", sez I,"This must be where non-blocking IO comes in". I read up on non-blocking IO, and this seems to be totally wrong.

But, while reading about non-blocking IO, I come across the idea of servers forking off processes to handle the details.
I've made a few attempts on this, but I can still maintain only one connection. Could someone elucidate me on this concept?
Below is the script that I'm using as a "base", and as a final note, I'm working on win32 (with fork capabilities, so that's not the issue, mmkay? :) )

#!/usr/bin/perl -w use IO::Socket; use Net::hostent; # for OO version of gethostbyaddr $PORT = 23; # pick something not in use $crlf = "\015\012"; $server = IO::Socket::INET->new(Proto => 'tcp', LocalPort => $PORT, Listen => 10, #SOMAXCONN, Reuse => 1); die "can't setup server" unless $server; print "[Server $0 accepting clients]\n"; while ($client = $server->accept()) { $client->autoflush(1); print $client "Welcome to $0; type help for command list.$crlf"; $hostinfo = gethostbyaddr($client->peeraddr); printf "[Connect from %s]\n", $hostinfo->name || $client->peerhost +; print $client "Command? $crlf"; while ( <$client>) { print "got $_";; next unless /\S/; # blank line if (/quit|exit/i) { last; + } elsif (/date|time/i) { printf $client "%s$crlf", scalar loc +altime; } elsif (/who/i ) { print $client $client->peerhost,$cr +lf; } elsif (/cookie/i ) { print $client "cookie$crlf"; } elsif (/motd/i ) {print "showing motd\n"; print $clien +t "motd$crlf"; } else { print $client "Commands: quit date who cookie motd$crlf"; } } continue { print $client "Command? $crlf"; } close $client; }

In reply to Creating a server that accepts more than one client. by boo_radley

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.