Sockets - client unable to connect

Ask a Question related to PERL Modules, Design and Development.

  1. #1

    Default 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:
    >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.

    raghu Guest

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default Re: Sockets - client unable to connect

    "raghu" <raghu_verabelli@yahoo.com> 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.
    > }
    <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

  4. #3

    Default 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

  5. #4

    Default Re: Sockets - client unable to connect

    "raghu" <raghu_verabelli@yahoo.com> 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

    --
    -------------------- [url]http://NewsReader.Com/[/url] --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    xhoster@gmail.com Guest

  6. #5

    Default 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

  7. #6

    Default Re: Sockets - client unable to connect

    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

    --
    $/=$,,$_=<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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139