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)
Any help here would be sooo apprecaited.#!/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); }
In reply to io::select bogs cpu on disconnect by Visionary
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |