ecuguru has asked for the wisdom of the Perl Monks concerning the following question:

I'm going to have a topology of 5 laptops reporting back to a central laptop. Each laptop will open a socket to port 2000-2005 respectively.

I'm trying to figure out the best way to have 5 <and eventually 25> open sockets in perl and efficiently manage the live data coming in.

Any topology advice, cpan module, what not, is appreciated.. Because I have been getting burned with random Broken Pipe messages, I'm very eager to hear some advice on how others are doing this <correctly> and how they would go about it.

5 open sockets waiting for connections on ports 2000 and up. data comes in from remote devices. how do u make sure that if there is a broken pipe that the connection gets reopened rather then left hanging unable to bind back to the port?
many thanks,

Replies are listed 'Best First'.
Re: multiple socket servers
by ozone (Friar) on Dec 21, 2004 at 12:17 UTC
    what would probably work best for you is POE, a form of user-space threading that I suspect is exactly what you're looking for.

    It is a bit of a learning curve, but there's a fair amount of example code around and I think The Camel Book or The Cookbook has some useful examples

      Also there is Net::Server, both have their places and both work very well. Net::Server has a shorter learning curve in my experience.
Re: multiple socket servers (use threads)
by BrowserUk (Patriarch) on Dec 22, 2004 at 06:27 UTC

    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