[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


connections left open with IO:select

Posted on 1998-09-21
Medium Priority
Last Modified: 2008-02-01
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";

Question by:kertis
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Author Comment

ID: 1204936
Edited text of question

Author Comment

ID: 1204937
Adjusted points to 170

Expert Comment

ID: 1204938
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).
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 1204939
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!

Expert Comment

ID: 1204940
What do you have against forking?

Accepted Solution

thoellri earned 800 total points
ID: 1204941
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";


Expert Comment

ID: 1204942
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.

Author Comment

ID: 1204943
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question