1. You want to get rid of the busy-wait in the client.

Currently you spawn a thread for sock_io() and a second thread for stdio_io(). Then you run a busy loop in your main thread.

You've identified that your main thread is excess. You can get rid of one thread (not the main one) by simply calling stdio_io() from the main thread rather than spawn a new thread. That is:

...; my $thr1 = threads->new(\&sock_io); stdio_io(); ...;

You should let stdio_io() return if the user presses Ctrl+D (Unix) or Ctrl+Z (DOS). Cleaning up the sock_io() thread when the user signals they're done is left as an exercise for you. :)

2 & 4. The first line entered by the user in the client is not sent to the server.

I'm taking a wild stab here by guessing it's a buffering issue. Turn buffering off on STDOUT and $socket. Before creating your thread(s):

...; use IO::Handle; STDOUT->autoflush(1); $socket->autoflush(1); ...;

3. Lines from STDIN have additional newlines, which may not be network newlines.

You need to chomp $msg_out before you print it to the socket:

while (<STDIN>) { chomp; print $socket $_ . CRLF; }

Additional question: Is learning/reading Unix Network Programming useful?

As with most things, there's a trade-off. Depth of knowledge is generally good, but it takes time. On the other hand, you could spend the time learning high-level libraries and using them to do more interesting things. It's really up to your personal preference or goals.

I started off low-level: I learned C before Perl. I wrote POE after learning about BSD sockets. I learned CGI, and now I use things like Catalyst. The novelty of low-level work wears off (for me) after about three uses.


In reply to Re: problem with sockets and CRLF and threads by rcaputo
in thread problem with sockets and CRLF and threads by spx2

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.