Solved

connections left open with IO:select

Posted on 1998-09-21
8
253 Views
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 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;
            }
        }
    }

0
Comment
Question by:kertis
8 Comments
 

Author Comment

by:kertis
ID: 1204936
Edited text of question
0
 

Author Comment

by:kertis
ID: 1204937
Adjusted points to 170
0
 
LVL 2

Expert Comment

by:bertvermeerbergen
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).
0
 

Author Comment

by:kertis
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!
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Expert Comment

by:b2pi
ID: 1204940
What do you have against forking?
0
 
LVL 5

Accepted Solution

by:
thoellri earned 200 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';

nonblock($server);
$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();
            $select->add($client);
            nonblock($client);
        } 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};

                $select->remove($client);
                close $client;
                next;
            }

            $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) {
        handle($client);
    }

    # 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";
            next;
        }
        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};

            $select->remove($client);
            close($client);
            next;
        }
    }

    # 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";
}

0
 
LVL 2

Expert Comment

by:bertvermeerbergen
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.
0
 

Author Comment

by:kertis
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
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now