spx2 has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: problem with sockets and CRLF and threads
by rcaputo (Chaplain) on May 24, 2007 at 03:23 UTC | |
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:
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):
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:
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. | [reply] [d/l] [select] |
by spx2 (Deacon) on May 25, 2007 at 23:25 UTC | |
| [reply] |
|
Re: problem with sockets and CRLF and threads
by NetWallah (Canon) on May 24, 2007 at 03:39 UTC | |
This is likely the reason you lose data you are expecting at the server. You probably need to change "while(<$session>)" to "while (my $msgout=<>)" in your "sub sock_io". Also, I suggest you do a blocking accept in your server, to avoid the busy loop (Research on this left as an exercise). "An undefined problem has an infinite number of solutions." - Robert A. Humphrey "If you're not part of the solution, you're part of the precipitate." - Henry J. Tillman | [reply] |
by spx2 (Deacon) on May 25, 2007 at 23:27 UTC | |
| [reply] |
|
Re: problem with sockets and CRLF and threads
by BrowserUk (Patriarch) on May 24, 2007 at 13:26 UTC | |
If you would take the time to format your post in a readable fashion. it might encourage more people to read it, and perhaps offer advice. If you would take the time to format your code better, we might get the impression that you are serious about learning something. What is it that you are trying to do? Attempting to learn all three things simultaneously is like trying to learn to drive in an articulated lorry (18-wheeler) in the fast last of the motorway (freeway) whilst trying to learn enough CB-radio lingo to be able to ask for directions to your destination. It's not just harder for you to learn; it taxes the patience of those that might elect to help you. Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |