You certainly don't need to use threads. Threads work OK if your platform is Windows, but for what you need a select()-based interface is preferable.

IO::Select (and other similar modules) allow you to watch one or more sockets (usually instances IO::Socket::INET) to see whether data is available for reading, the outgoing socket buffer isn't full, or whether the sockets are disconnected.

You simply have a loop with a call to select() passing some reasonable timeout (say 50 milliseconds).

On the client side you want to be able to read and write at the same time. Buffer reads from the client into a scalar variable until the buffer contains a newline (or whatever is your end-of-message marker).

Don't write to the server until you have a complete message.

If you want it to look like IRC, you'll probably have to update the screen nicely somewhere during that select() loop. There's only one thread so you've no worries about multiple threads drawing at the same time. That means you can't use simple reading functions or <STDIN>; take a look at Term::ReadKey to see how to get one key at a time.

On the server you'll also need a select() loop, but instead of one socket, you'll have a socket for each connected client. Also, you'll have a listen socket in the select-group which will be available for reading when a client is connecting (you should call accept()).

Buffer both reads and writes from each client separately. When you get a full message from a client, prepend it to the write buffer of all clients (don't skip the sender :-).

Add some logic on the client and server for cleanly disconnecting (and perhaps noticing when the connection goes away due to errors) and you're done.

-David

Update: oops, that "can" must've been confusing :-)


In reply to Re: perl socket comunication by erroneousBollock
in thread perl socket comunication by wavenator

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.