Since I am trying to learn, please do not recommend modules that do this for me. I have looked at Net::Server and it is way over my head at this point. Additionally, I have RTFM'd so if there is something I am missing in the docs, please be specific if you recommend further reading.
#!/usr/bin/perl use strict; use warnings; use IO::Select; use IO::Socket::INET; my $hsock = IO::Socket::INET->new ( Listen => 5, LocalAddr => '0.0.0.0', LocalPort => 5885, Proto => 'tcp', Type => SOCK_STREAM, Reuse => 1 ); my $handles = IO::Select->new($hsock); my %user; while (1) { last if keys %user > 4; my @ready = $handles->can_read(3); for my $sock (@ready) { if ($sock eq $hsock) { my $new_sock = $hsock->accept(); $handles->add($new_sock); $new_sock->send("Welcome\r\n"); $_->send("New user joined\r\n") for values %user; $user{$new_sock} = $new_sock; } else { my $buff = <$sock>; if ($buff) { $buff =~ tr/\r\n//d; next if $buff =~ /^\s*$/; $_->send("$buff\r\n") for values %user; } else { $handles->remove($sock); delete $user{$sock}; $_->send("User left\r\n") for values %user; } } } }
This code works - sorta. The timing of output is off. It seems as though the handles do not flush automatically even though auto-flush is on by default for socket handles. I considered forking the clients as I saw in other examples, but I want to understand why what I have doesn't work. It think I just have a stupid flow control problem between accept(), send(), and receive().
It seems to me that with just a listener and a couple of clients, even a single process should be able to keep up. I would prefer my existing code be modified to work with an explanation, but as always - all help/advice is appreciated.
Update: It may have been the client!
I received a /msg from BrowserUk indicating that from a windows telnet session it appeared to work just fine. I had been using Putty. When I changed the Putty protocol from "telnet" to "raw", it worked as BrowserUk described. I still welcome any/all advice!
Cheers - L~R
In reply to Timing issues with IO::Socket::INET and IO::Select by Limbic~Region
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |