I'm going nuts over here. And now that I've almost pulled out all the hair - so I've got to ask for a hand.

I've got a sever background process running. It accepts connections, does its stuff and works fine... Unless a client gets disconnected without logging out.

If a client losses his connection to the server, in a matter of 5 seconds the server process will be using 95 - 99% of the CPU. I don't know whats causing the sudden pig behavor.... I've tried all kinds of error checking (and dropping clients that seem bad) but to no avail.

It should be noted, that hundreds of clients can connect and logout properly and this server doesn't even register on TOP... But the second one drops its connection... BOOM.

Here is my code: (please excuse the mess)

#!/usr/bin/perl -w use strict; use IO::Socket; use IO::Select; use POSIX; # Become a daemon my $pid = fork; exit if $pid; die "Couldn't fork: $!" unless defined($pid); POSIX::setsid() or die "Can't start a new session: $!"; my ($msg, $nread, $in, $message); my $max_msglen = 1024; my $max_clients = 1; my $port = 9000; $0 = "hcdemo: Accepting clients on port $port"; my $new_client = IO::Socket::INET->new(Proto=>"tcp", LocalPort=>$p +ort, Listen=>$max_clients, Reuse=>1); my $sel = IO::Select->new($new_client); print "listening at port $port\n"; main(); sub main { # ------------------------------------------------------------------- # while (my @ready = $sel->can_read) { foreach my $client (@ready) { if ($client == $new_client) { # New connection my $add = $client->accept; $sel->add($add); # $msg = "LIVEHELP\n"; # syswrite($add, $msg, length($msg)); } else { # Existing connection $msg = ""; $nread = sysread($client, $msg, $max_msglen); chop($msg); chop($msg); if ($msg =~ /alex/i) { $msg = "Other user says to kiss his ass\n"; foreach ($sel->can_write) { if ($_ ne $client) { syswrite($_, $msg, length($msg)); } } } elsif ($msg =~ /end/i) { my($end, $msg) = split(/\|/,$msg); foreach ($sel->can_write) { if ($_ ne $client) { syswrite($_, $msg, length($msg)); } } $sel->remove($client); } elsif ($msg =~ /update/i) { my $users="Ususally I sub to count users"; $msg = "$users\n"; syswrite($client, $msg, length($msg)); } elsif ($nread) { foreach ($sel->can_write) { if ($_ ne $client) { if (!syswrite($_, $msg, length($msg))) { $sel->remove($_); } } } } } } } } sub sendall { my ($msg) = @_; foreach ($sel->can_write) { if (!syswrite($_, $msg, length($msg))) { $sel->remove($_); } } } sub getmsg { my ($handle) = @_; my $msg = ""; do { $nread = sysread($handle, $in, 1024); $msg .= $in; } while ($nread > 0); return($msg); }
Any help here would be sooo apprecaited.

Thanks,

Alex

In reply to io::select bogs cpu on disconnect by Visionary

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.