SERVER ------use IO::Socket; use IO::Select; use Time::HiRes; use strict; for (1..500) { my $socket = IO::Socket::INET->new("localhost:12002") or die "no s +ocket"; print $socket "this is a test 123\n"; print $socket "this is a test 456\n"; Time::HiRes::sleep(.5); $socket->close() or die "error closing"; }
use IO::Socket; use IO::Select; use Time::HiRes; use strict; my $listen_socket; my $readers; my %sessions; CreateListener(); while (1) { Poll(); print "."; Time::HiRes::sleep(.08); } sub CreateListener() { $listen_socket = IO::Socket::INET->new(LocalPort => 12002, Proto => 'tcp', Reuse => 1, Listen => 50); $listen_socket->blocking(0); $readers = IO::Select->new($listen_socket); $readers->add($listen_socket); } sub Poll { my @ready = $readers->can_read(0); return if (!@ready); my $handle = shift @ready; if ($handle) { if ($handle eq $listen_socket) { my $connect = $listen_socket->accept(); $sessions{"$connect"}{status} = 'first_connect'; $readers->add($connect); } else { my $user_input; my $bytes = sysread($handle, $user_input, 16384); if ($bytes && $bytes > 0) { chomp($user_input); if ($sessions{"$handle"}{status} eq "first_connect") { # Save data until socket is closed... $sessions{"$handle"}{status} = "next_connect"; $sessions{"$handle"}{data} = $user_input; } else { # Append data until socket is closed... $sessions{"$handle"}{data} .= $user_input; } } if (!$bytes) { # Got it all print $sessions{"$handle"}{data} . "\n"; # Close this socket $readers->remove($handle); delete $sessions{"$handle"}{status}; delete $sessions{"$handle"}{data}; close $handle; undef $handle; } } } }
In reply to nonblocking but leaking... by smackdab
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |