Ask a Question related to PERL Modules, Design and Development.
-
raghu #1
Sockets - client unable to connect
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:
C:\>perl server2.pl>From Server terminal window:
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
C:\>perl client1.pl>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
cannot connect to server at client1.pl line 9.
raghu Guest
-
Unable to connect to FMS from outside of the US
I'm having a really strange problem. We set up our FMS a couple of weeks ago, and are able to stream videos. However, the person that is directing... -
Help me: Unable to connect to FMS
Hello Guys I have an audio recorder . It is working fine on localhost but it is unable to connect to the remote FMS . i have tried... -
cannot connect to accepting sockets...
hello all, I am writing a Gtk (well Gtkmm actually) version of othello that has aspirations of being networkable, but I am having a little... -
Unable to connect to SQL 2K via VS.NET
Hi all, As the subject says I'm having trouble connecting to SQL Server 2K via Visual Studio.NET 2002 (VB). It tells me that SQL Server does not... -
Unable to connect
Hello, I have problems with connecting to SQL Server from ESQL/C application. Error message is: SQL Server Message 19703: Unable to connect: SQL... -
xhoster@gmail.com #2
Re: Sockets - client unable to connect
"raghu" <raghu_verabelli@yahoo.com> wrote:
Is the server still running at this point, or is it no longer running?> 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.
....I rather dislike this idiom. accept should never return false except>
> while ($client = $server->accept()) {
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 !=$!, @=$@";
Make that> $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;
close $client or die "$!"
I don't expect it to reveal the problem, but you never know.
<End of program>> }
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
--
-------------------- [url]http://NewsReader.Com/[/url] --------------------
Usenet Newsgroup Service $9.95/Month 30GB
xhoster@gmail.com Guest
-
raghu #3
Re: Sockets - client unable to connect
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.
raghu Guest
-
xhoster@gmail.com #4
Re: Sockets - client unable to connect
"raghu" <raghu_verabelli@yahoo.com> wrote:
What is SOMAXCONN on your system?> 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.
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
--
-------------------- [url]http://NewsReader.Com/[/url] --------------------
Usenet Newsgroup Service $9.95/Month 30GB
xhoster@gmail.com Guest
-
raghu #5
Re: Sockets - client unable to connect
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.
raghu Guest
-
Thomas Kratz #6
Re: Sockets - client unable to connect
raghu wrote:
Just for the records,> 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.
SOMAXCONN under windows should is 0x7fffffff in perl 5.8.8 as the winsock2
header files are used.
Thomas
--
$/=$,,$_=<DATA>,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^.-
Thomas Kratz Guest



Reply With Quote

