You can't tell how many are waiting to be accepted, but you can tell if there are "zero" or "one or more" waiting. You don't provide much detail, but I assume you might be asking this so that you can avoid blocking on accept(). If that's the case, this example might be helpful.

#!/usr/bin/perl use strict;use warnings; use IO::Select; use IO::Socket; use Data::Dumper; $Data::Dumper::Terse = 1; $Data::Dumper::Useqq = 1; $Data::Dumper::Indent = 0; # unbuffer STDOUT so you can watch the output real time select STDOUT;$| = 1; # create a listen socket, on port 7777 my $l = IO::Socket::INET->new( Listen => 1, LocalPort => 7777, ReuseAddr => 1 ) or die("Can't listen: $!\n"); # create an IO::Select object my $sel = IO::Select->new($l); my $timeout = 0.1; my @waiting; while (1) { # @waiting -> sockets with "something ready to read", # (including the listen socket) @waiting = $sel->can_read($timeout); my $nwaiting = scalar(@waiting); printf "%d sockets are readable...\n", $nwaiting; if ($nwaiting) { foreach my $fh (@waiting) { if ( $fh == $l ) { # listener has a new connection for us... print "\tlistener accepting connection..\n"; my $newconn = $l->accept(); # add this new connection to the select mask $sel->add($newconn); } else { # one of the connected clients wrote something to us print "\tsocket has unread data\n"; my $buf; # read 255 byte chunk. if they sent more # than that, we'll get it the next go-round if ( $fh->sysread( $buf, 255 ) ) { printf "\tgot [%s] from socket\n", Dumper($buf); } else { # client disconnected print "\t client gone, closing socket\n"; $fh->close(); $sel->remove($fh); } # read from $fh here... } } } else { print "\tyou could do other stuff here...\n"; } # sleep to avoid a tight loop # probably too long for a real app, but useful # to watch STDOUT in real time sleep(2); }

In reply to Re: How to count the number of pending clients in socket queue? by kschwab
in thread How to count the number of pending clients in socket queue? by mrbark

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.