use strict; use warnings; use IO::Select; use IO::Socket; $|=1; $SIG{INT}=\&exit_genchars; my $time_zone_inc=10; my %sock_client_hash; my $cc=48; my $server_sock = new IO::Socket::INET( Listen => 1,LocalPort => 19,Reuse=> 1 ); my $sel = new IO::Select( $server_sock ); my @ready; while($server_sock) { @ready = $sel->can_read(0.0001); foreach my $socket (@ready) { if($socket == $server_sock ) { new_socket($socket); }else{ if(defined ($socket)) { close_socket($socket); } } } my @wready = $sel->can_write(0.0001); my $wsocket; foreach $wsocket (@wready) { gen_chars($wsocket); } } sub new_socket { my $newclientsock=shift; $newclientsock = $server_sock ->accept; $sel->add($newclientsock); my ($ip,$peer_port)=sock_attrs($newclientsock); $sock_client_hash{$newclientsock->fileno}{ip}=$ip; $sock_client_hash{$newclientsock->fileno}{port}=$peer_port; my $fileNo=$newclientsock->fileno; log_event("New Client connected -> FileNo($fileNo) $ip:$peer_port\ +n"); } sub gen_chars { my($wrs) = $_[0]; if($cc==58) { $cc=65; }elsif($cc==91) { $cc=97; }elsif($cc==123) { $cc=48; } $wrs->send(chr($cc)) or close_socket($wrs); $cc++; } sub close_socket { my $socket=$_[0]; my $sock_ip=$sock_client_hash{$socket->fileno}{ip}; my $sock_peer_port=$sock_client_hash{$socket->fileno}{port}; my $fileNo=$socket->fileno; log_event("Unable to write to -> FileNo($fileNo) $sock_ip:$sock_pe +er_port\n"); if(defined ($socket)) { $sel->remove($socket); $socket->close; log_event("Removed socket -> FileNo($fileNo) $sock_ip:$sock_pe +er_port\n"); } } sub sock_attrs { my $socket=$_[0]; my $ip=$socket->peerhost; my $port=$socket->peerport; return $ip,$port; } sub log_event { my $msg=shift; my $gmTime=rTime(); print "$gmTime -> $msg"; } sub rTime { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime(time); my $militaryTime=($hour)+$time_zone_inc; my $m; my $s; if($militaryTime>24) { $militaryTime=$militaryTime-24; } $militaryTime=$hour; if(length($min)==1) { $m="0".$min; $min=$m; } if(length($sec)==1) { $s="0".$sec; $sec=$s; } my $roundedTime="$militaryTime:$min:$sec"; return $roundedTime; } sub exit_genchars { log_event("Exit called\n"); exit(0); }

The processing/controlling of incoming sockets whilst also outputting to connected sockets needs to be handled better.
But with my while loop, with one client connected, it is still far too slow. I want to avoid spawning a new process at any cost.

In reply to chargen program is too slow / IO::Select socket handle outputting timing issue by kabeldag

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.