All,
I am finally getting around to one of my "To-Learn" items - network programming. While my ultimate project is a bit bigger than this simple chat server, I can't seem to get even the basics working.

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

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.