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

Hello all. I am seeking knowledge with respect to setting up an IO::Socket Server that will have longevity. I mainly want to understand why the version I am posting below does not allow more than ~6,000 connects from a client over and over again. I have the client close the socket between reconnects. I really want to understand anything I am doing wrong so I can setup a server that will stay up and running for quite some time. I want it to survive many connections (possibly into the millions, but not at the same time).

I am running this on a Windows XP system using ActiveState Perl. I will eventually run this on a Linux box.

Any help would be greatly appreciated. Thank you in advance for your help.

=================================
use strict;
use warnings;

use IO::Socket;

my $port = 22000;

print STDOUT "Starting Server on Port $port\n";

StartServer();

sub StartServer{
my $socket = IO::Socket::INET->new(
LocalAddr => 'localhost',
LocalPort => $port,
Proto => 'tcp',
Listen => 5,
Reuse => 1,
Type => SOCK_STREAM,
) or die "Can NOT open socket for server\n\tReason: $@\n\n";

for(;;){
$socket->autoflush(1);
my $client = $socket->accept();

while (my $Request = <$client>){

print STDOUT "Request: $Request\n";
print $client "Your request $Request\n";
}
}
}

Replies are listed 'Best First'.
Re: Long Life IO::Socket Server
by lostjimmy (Chaplain) on Sep 11, 2009 at 19:24 UTC

    Are you getting any error messages? It would be immensely helpful if we could have those.

    You should also check for success on the accept() call: my $client = $socket->accept() or die "accept failed: $!";. Also, in your die statement, you want $! (system call errors), not $@ (eval errors).

Re: Long Life IO::Socket Server
by Khen1950fx (Canon) on Sep 12, 2009 at 00:59 UTC
    I tried it along these lines:

    #!/usr/bin/perl use strict; use IO::Socket; use Net::hostent; my $port = 22500; my $server = IO::Socket::INET->new( LocalAddr => 'localhost', LocalPort => $port, Proto => 'tcp', Type => SOCK_STREAM, Listen => SOMAXCONN, Reuse => 1 ) or die "Can't setup server: $!\n"; print "[Server $0 accepting clients]\n"; my $request; while ( my $client = $server->accept() ) { my $client->autoflush(1); my $hostinfo = gethostbyaddr( $client->peeraddr ); print $client "$request: \n"; while ( $request = <$client> ) { next unless /\S/; if (<$client>) { print $client "$request\n"; } else { close($client); } }