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


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

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


$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
    while(@ready = $sel->can_read) {
        foreach $fh (@ready) {
            if($fh == $lsn) {
                # Create a new socket
                $new = $lsn->accept;
                        print "opening on fileno ".$new->fileno."!\n";
            else {
                # Process socket

                        # only a dummy receive and send

                        print " receiving on fileno ".$fh->fileno."!\n";

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

                        print " sending on fileno ".$fh->fileno."!\n";

                        # Maybe we have finished with the socket
                        print " closing on fileno ".$fh->fileno."!\n";

Who is Participating?
thoellriConnect With a Mentor Commented:
Nah - doesn't work this way! If you plan to do a non-forking server then you have to split up processing of reading and writing in different portions of your code. You should really try the following script (which is taken as is from the Perl-Cookbook - by the way excellent reading material).

#!/usr/bin/perl -w
# nonforker - server who multiplexes without forking
use POSIX;
use IO::Socket;
use IO::Select;
use Socket;
use Fcntl;
use Tie::RefHash;

$port = 1685;               # change this at will

# Listen to port.
$server = IO::Socket::INET->new(LocalPort => $port,
                                Listen    => 10 )
  or die "Can't make server socket: $@\n";

# begin with empty buffers
%inbuffer  = ();
%outbuffer = ();
%ready     = ();

tie %ready, 'Tie::RefHash';

$select = IO::Select->new($server);

# Main loop: check reads/accepts, check writes, check ready to process
while (1) {
    my $client;
    my $rv;
    my $data;

    # check for new information on the connections we have

    # anything to read or accept?
    foreach $client ($select->can_read(1)) {

        if ($client == $server) {
            # accept a new connection

            $client = $server->accept();
        } else {
            # read data
            $data = '';
            $rv   = $client->recv($data, POSIX::BUFSIZ, 0);

            unless (defined($rv) && length $data) {
                # This would be the end of file, so close the client
                delete $inbuffer{$client};
                delete $outbuffer{$client};
                delete $ready{$client};

                close $client;

            $inbuffer{$client} .= $data;

            # test whether the data in the buffer or the data we
            # just read means there is a complete request waiting
            # to be fulfilled.  If there is, set $ready{$client}
            # to the requests waiting to be fulfilled.
            while ($inbuffer{$client} =~ s/(.*\n)//) {
                push( @{$ready{$client}}, $1 );

    # Any complete requests to process?
    foreach $client (keys %ready) {

    # Buffers to flush?
    foreach $client ($select->can_write(1)) {
        # Skip this client if we have nothing to say
        next unless exists $outbuffer{$client};

        $rv = $client->send($outbuffer{$client}, 0);
        unless (defined $rv) {
            # Whine, but move on.
            warn "I was told I could write, but I can't.\n";
        if ($rv == length $outbuffer{$client} ||
            {$! == POSIX::EWOULDBLOCK) {
            substr($outbuffer{$client}, 0, $rv) = '';
            delete $outbuffer{$client} unless length $outbuffer{$client};
        } else {
            # Couldn't write all the data, and it wasn't because
            # it would have blocked.  Shutdown and move on.
            delete $inbuffer{$client};
            delete $outbuffer{$client};
            delete $ready{$client};


    # Out of band data?
    foreach $client ($select->has_exception(0)) {  # arg is timeout
        # Deal with out-of-band data here, if you want to.

# handle($socket) deals with all pending requests for $client
sub handle {
    # requests are in $ready{$client}
    # send output to $outbuffer{$client}
    my $client = shift;
    my $request;

    foreach $request (@{$ready{$client}}) {
        # $request is the text of the request
        # put text of reply into $outbuffer{$client}
    delete $ready{$client};

# nonblock($socket) puts socket into nonblocking mode
sub nonblock {
    my $socket = shift;
    my $flags;

    $flags = fcntl($socket, F_GETFL, 0)
            or die "Can't get flags for socket: $!\n";
    fcntl($socket, F_SETFL, $flags | O_NONBLOCK)
            or die "Can't make socket nonblocking: $!\n";

kertisAuthor Commented:
Edited text of question
kertisAuthor Commented:
Adjusted points to 170
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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).
kertisAuthor Commented:
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?
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.
kertisAuthor Commented:
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/ 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.