Link to home
Start Free TrialLog in
Avatar of kertis
kertis

asked on

connections left open with IO:select

Hi there!
I have problems with connections left open by my little server.
If you run it on your system and start a browser requesting http://127.0.0.1:1234 everything is all right.
But when you do multiple requests (just hold the enter key) you have some open connections, but the old ones are not handled by my server anymore, because you can't read on these connections (the browser has closed the connection already).

Do you have an idea (without forking), how I can get rid of these dead connections? I hope, there's an easy and fast running solution.

#!/usr/bin/perl

    use IO::Select;
    use IO::Socket;

    $lsn = new IO::Socket::INET(Proto => 'tcp', Listen => 1, LocalPort => 1234);
    $sel = new IO::Select( $lsn );

$|=1;

$i=1;
$msg="HTTP/1.0 200 OK\nContent-type: text/html\n\n<html></html>";
while ($i<5000)
{$msg.="<b>test</b> <i>testasdfjklsa j ajskdl</i>\n"; # some test output
$i++;
}
   
    while(@ready = $sel->can_read) {
        foreach $fh (@ready) {
            if($fh == $lsn) {
                # Create a new socket
                $new = $lsn->accept;
                $sel->add($new);
                        print "opening on fileno ".$new->fileno."!\n";
            }
            else {
                # Process socket

                        # only a dummy receive and send

                        print " receiving on fileno ".$fh->fileno."!\n";
                        $fh->recv($test,1000);

                        print " processing for fileno ".$fh->fileno."!\n";
                        sleep 1; # processing the input may take a while

                        print " sending on fileno ".$fh->fileno."!\n";
                $fh->send($msg);

                        # Maybe we have finished with the socket
                        print " closing on fileno ".$fh->fileno."!\n";
                $sel->remove($fh);
                $fh->close;
            }
        }
    }

Avatar of kertis
kertis

ASKER

Edited text of question
Avatar of kertis

ASKER

Adjusted points to 170
Have the 'old ones' been accepted by the server ?  If so, what do mean by 'you can't read on these' ?  Normally, the (next) read on these connections will return a 0-length message, indicating that the remote side of the connection has been closed down (by the browser, in this case).
If you mean that some of your browser connections never got accepted by the server, you need to increase the listen queue size (set to '1' in your server, this will almost certainly reject some connections requested by the browser when you hold down the enter key).  Especially when implementing servers handling multiple connections without forking, the queue size should be set higher (try '5' or so).
Avatar of kertis

ASKER

All connections have been accepted by the server. But the problem is, that after opening connections ($fh == $lsn) not every connection still is a connection included in $sel->can_read, because having been closed by the browser. So these opened connections will never get into the "else" part (not new and readable).
I need a way to sort out and get rid of these dead connections by the server. 200 Points now for the best answer!
What do you have against forking?
ASKER CERTIFIED SOLUTION
Avatar of thoellri
thoellri

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Do you really get different fileno's on your connections ?  If so, try to call the 'has_error' to see if these fd's are flagged in error.  You  will have to rearrange the while loop a bit. Make it loop forever and call $sel->has_error(0) first, than $sel->can_read.  Check for fd's set after the has_error call and remove those from the list.  (the 0 argument to has_error will poll and return instantly even if no fd is marked in error.
Avatar of kertis

ASKER

Thanx for your answer, thoellri - and sorry for not grading earlier, I've been moving during the last weeks.
I've bought the Perl Cookbook, too - it really IS great.

But I sometimes get a strange error:
Bad arg length for Socket::unpack_sockaddr_in, length is 0, should
 be 16 at /usr/lib/perl5/i586-linux/5.004/Socket.pm line 249.

Has anybody of you seen this error msg before and knows a way to avoid it? I'm trying to test when the error occurs. You can post your answer to my new question: Bad arg length for Socket::unpack_sockaddr_in in the Perl forum