It's pretty simple with threads. This seems to handle 100 ports with aplomb.

Multi-port server

#! perl -slw use strict; use threads; use threads::shared; use IO::Socket::INET; use Term::ReadKey; $| = 1; our $BASE ||= 2000; ## base port our $PORTS ||= 5; ## Number of consecutive ports to open our $STATUS ||= 1; ## decimal seconds between status updates my $running : shared = 0; my $stop : shared = 0; my %status : shared; sub listener { $running++; my( $port ) = @_; $status{ $port } = 'started'; my $socket = IO::Socket::INET->new( LocalPort => $port, Type => SOCK_STREAM, Listen => 1, Reuse => 1, Timeout => 10, ) or die "Error: $!\n"; until( $stop ) { while( my $client = $socket->accept ) { $status{ $port } = 'opened'; while( <$client> ) { chomp; $status{ $port } = 'Receiving'; print $client "$port: $_"; $status{ $port } = 'Replying'; } close $client; $status{ $port } = 'Closed'; } } $status{ $port } = 'Stopping'; $running--; } warn "Starting listeners\n"; my @listeners = map{ threads->new( \&listener, $_ )->detach } $BASE .. $BASE + $PORTS; sleep 1 while $running < @listeners; ## Giv'em a timeslice printf( "\r%s\t", join "\t", map{ "$_:$status{ $_ }" } sort keys %status ) and Win32::Sleep( $STATUS ) while $running and not defined( ReadKey( -1 ) ); $stop = 1; printf( "\r%s\tWaiting for:$running", join "\t", map{ "$_:$status{ $_ }" } sort keys %status ) and Win32::Sleep( $STATUS ) while $running; warn "\nAll done\n";

A client to test it

#! perl -slw use strict; use IO::Socket::INET; $| = 1; my $port = $ARGV[ 0 ] || die "No port number"; my $socket = IO::Socket::INET->new( PeerAddr => 'localhost', PeerPort => $port, Proto => "tcp", Type => SOCK_STREAM ) or die "Couldn't connect to localhost:$port : $!\n"; $socket->autoflush; for ( 1 .. rand 1000 ) { warn "Sending '$_'\n"; print $socket "$port: $_"; my $read = <$socket>; warn "Got '$read'\n"; Win32::Sleep rand 10; } close $socket;

I tested it by running upto 100 clients using a shell for loop:

for /l %i in (2000,1,2100) do @start /b perl 416434-c.pl %i

Examine what is said, not who speaks.        The end of an era!
"But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
"Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

In reply to Re: multiple socket servers (use threads) by BrowserUk
in thread multiple socket servers by ecuguru

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.