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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |