Sockets - client unable to connect
Sockets - client unable to connect
am 01.05.2006 23:28:50 von Raghu
Hi All,
I am new to Perl world. I wrote a simple client and server program
using sockets. Please see code below. Server accepts connection
request and sleeps for 2 minutes before processing the next client.
This works fine upto 6 clients. Once the limit reaches 6, I am unable
to start any more new clients i.e. clients fail to connect to the
server.
What could be wrong? Is there some limit on number of socket
connections in perl? Any suggestions how to resolve this issue?
Thanks in advance,
Raghu
server.pl:
#!C:/mod_perl/Perl/bin/perl.exe
use IO::Socket;
my $line;
my $server;
my $client;
my $count = 0;
$server = IO::Socket::INET->new( Proto => 'tcp',
LocalPort => 1234,
Listen => SOMAXCONN,
Reuse => 1);
print STDERR "Server started - accepting Clients\n";
while ($client = $server->accept()) {
$count++;
print STDOUT "Accepted New Client: $count\n";
while (defined ($request = <$client>)) {
print STDOUT "Processing new client's request:";
print STDOUT $request;
print STDERR "going to sleep\n";
sleep(120);
print STDERR "sleep completed\n";
}
close $client;
}
client1.pl
#!C:/mod_perl/Perl/bin/perl.exe
use IO::Socket;
$remote = IO::Socket::INET->new( Proto => "tcp",
PeerAddr => "localhost",
PeerPort => "1234",
);
unless ($remote) { die "cannot connect to server" }
print $remote "Hi, I am client..1..\n";
close $remote;
Outputs:
>From Server terminal window:
C:\>perl server2.pl
Server started - accepting Clients
Accepted New Client: 1
Processing new client's request:Hi, I am client..1..
going to sleep
sleep completed
Accepted New Client: 2
Processing new client's request:Hi, I am client..1..
going to sleep
sleep completed
Accepted New Client: 3
Processing new client's request:Hi, I am client..1..
going to sleep
sleep completed
Accepted New Client: 4
Processing new client's request:Hi, I am client..1..
going to sleep
sleep completed
Accepted New Client: 5
Processing new client's request:Hi, I am client..1..
going to sleep
sleep completed
Accepted New Client: 6
Processing new client's request:Hi, I am client..1..
going to sleep
sleep completed
>From client terminal window:
C:\>perl client1.pl
C:\>perl client1.pl
C:\>perl client1.pl
C:\>perl client1.pl
C:\>perl client1.pl
C:\>perl client1.pl
C:\>perl client1.pl
cannot connect to server at client1.pl line 9.
Re: Sockets - client unable to connect
am 02.05.2006 00:24:24 von xhoster
"raghu" wrote:
> Hi All,
>
> I am new to Perl world. I wrote a simple client and server program
> using sockets. Please see code below. Server accepts connection
> request and sleeps for 2 minutes before processing the next client.
> This works fine upto 6 clients. Once the limit reaches 6, I am unable
> to start any more new clients i.e. clients fail to connect to the
> server.
Is the server still running at this point, or is it no longer running?
....
>
> while ($client = $server->accept()) {
I rather dislike this idiom. accept should never return false except
on an (fatal?) error. So this is probably intended to be an infinite loop.
So make it look like one.
while (1) {
my $client = $server->accept() or die "Couldn't accept !=$!, @=$@";
> $count++;
> print STDOUT "Accepted New Client: $count\n";
>
> while (defined ($request = <$client>)) {
> print STDOUT "Processing new client's request:";
> print STDOUT $request;
> print STDERR "going to sleep\n";
> sleep(120);
> print STDERR "sleep completed\n";
> }
>
> close $client;
Make that
close $client or die "$!"
I don't expect it to reveal the problem, but you never know.
> }
And this is why I dislike the idiom. If the accept fails, you just
silently fall out of the while loop and end the program, without printing
the reason it failed. You could add an
die "Couldn't accept !=$!, @=$@";
right after the close of the while loop, but I like it better up top where
the accept is called in the first place.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
Re: Sockets - client unable to connect
am 02.05.2006 02:02:04 von Raghu
Thanks Xho for responding.
Yes, the Server is still running and continues to process client
requests as expected with an interval (sleep) of 2 minutes in between
each client. The problem is on client side. New clients cannot connect
after reaching certain limit on number of clients. After Server
completes processing of one client request, then I can start another
client successfully. It looks like to me like there is a limit of 5 at
the accept level. Server cannot have more than 5 clients pending for
acceptance at a time.
Re: Sockets - client unable to connect
am 02.05.2006 02:07:21 von xhoster
"raghu" wrote:
> Thanks Xho for responding.
>
> Yes, the Server is still running and continues to process client
> requests as expected with an interval (sleep) of 2 minutes in between
> each client. The problem is on client side. New clients cannot connect
> after reaching certain limit on number of clients. After Server
> completes processing of one client request, then I can start another
> client successfully. It looks like to me like there is a limit of 5 at
> the accept level. Server cannot have more than 5 clients pending for
> acceptance at a time.
What is SOMAXCONN on your system?
perl -le 'use IO::Socket; print SOMAXCONN'
128
Or on windows, probably run as:
perl -le "use IO::Socket; print SOMAXCONN"
I'm going to guess that it is 5 for you. Thus, no more then 5 connections
can be pending accepts. This is an OS thing.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
Re: Sockets - client unable to connect
am 02.05.2006 05:45:31 von Raghu
Excellent. Thanks very much Xho for the clue. I was aware that there is
some limit (128) for number of sockets a process can open, but it did
not expect it to be so low on windows. You are right, it is 5 on
windows.
Our production is on MacOS and the reason for which we were seeing the
same problem there was bug a in our actual app code. Our developer used
string literals for 'SOMAXCONN':
my ($proto,$listen,$timeout,$reuse) = ('tcp','SOMAXCONN',120,1);
As a result of this, we were seeing same connection failures after
starting about 9-10 clients on MacOS. Once we removed the string
literals, it is working as expected.
thanks very much for your help.
Re: Sockets - client unable to connect
am 02.05.2006 19:36:16 von Thomas Kratz
raghu wrote:
> Excellent. Thanks very much Xho for the clue. I was aware that there is
> some limit (128) for number of sockets a process can open, but it did
> not expect it to be so low on windows. You are right, it is 5 on
> windows.
Just for the records,
SOMAXCONN under windows should is 0x7fffffff in perl 5.8.8 as the winsock2
header files are used.
Thomas
--
$/=$,,$_=,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~...... >r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k ^.-