This is not normally how you would write a multi-client server. Your code accepts a connection, waits up to 10 seconds for data to arrive, sends the data back if it arrives in time, then disconnects and goes back to wait for the next connection. This is handling clients serially. If I were to attempt 60 connections to your server but not send any data on any of those connections, any other clients would be hung for up to 10 minutes before you to timed out all of my requests.

Normally, the server socket would be included in the select set. When that socket becomes ready, you can issue an accept on it. If another socket becomes ready, you can issue a read on it. Timeout handling becomes a little more difficult.

I just hacked this up and it seems to work.

#!/usr/bin/perl -w use strict; use IO::Select; use IO::Socket; use constant ECHO_PORT => 9999; my %clients; my $count; my $srvr = IO::Socket::INET->new(Proto=>'tcp', LocalPort=>ECHO_PORT, Listen=>SOMAXCONN, Reuse=>1) or die "Error creating server socket: $!"; $srvr->autoflush(1); my $sel = IO::Select->new($srvr); while (1) { my @rdy = $sel->can_read(1); if (scalar @rdy == 0) { # remove timed out clients my $t = time(); for my $cli (keys %clients) { if ($clients{$cli}{timeout} <= $t) { $sel->remove($clients{$cli}{conn}); $clients{$cli}{conn}->close(); delete $clients{$cli}; } } } for my $cli (@rdy) { if ($cli == $srvr) { my $cli = $srvr->accept; unless ($cli) { die "Error accepting client connection: $!\n"; next; } $clients{$cli}{conn} = $cli; $clients{$cli}{timeout} = time() + 10; $sel->add($cli); } else { my $buffer; if (sysread ($cli,$buffer,1024)) { syswrite($cli,$buffer); } else { warn "Error reading from client: $!\n"; } $sel->remove($cli); $cli->close(); delete $clients{$cli}; } } }
--- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';

In reply to Re: socket - echo server by pfaut
in thread socket - echo server by Anonymous Monk

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.