spx2 has asked for the wisdom of the Perl Monks concerning the following question:

___
  • Comment on problem with sockets and CRLF and threads

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:

    ...; 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.

      thank you very much for the
      reply,it has been very insightful
Re: problem with sockets and CRLF and threads
by NetWallah (Canon) on May 24, 2007 at 03:39 UTC
    Your SERVER code has two "<$session>", one in each thread.

    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

      your reply also helped me figgure out my problem
      as i was printing to the session and then starting to read it
      wich was obviously wrong...also becuase $session was used
      in both threads i locked it in each of them so i could
      use it safely and shared it between them using
      threads::shared
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?

    • Get a chat application to use?

      Download one of the zillion, working, tested and available for free.

    • Learn Perl.

      Start with something simpler than a multi-tasking, multi-communicating application.

    • Learn socket programming.

      Start with something non-multi-tasking.

      Understand the basics of stream IO.

      Understand what CRLF is and how it relates to

      1. network conventions;
      2. platform conventions;
      3. Perl's IO operators.

    • Learn threaded programming.

      Start with something that doesn't involve stuff that you don't yet understand like Perl's basic IO operators. Never mind socket programming.

    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.

    1. Badly formatted post:

      means few people will bother to read it.

    2. Badly formatted code:

      He wants to know how to pull a J-turn whilst 4-wheel drifting, but doesn't understand the difference between under- and over-steer yet.

    3. Questions that go off in a dozen different directions:

      Eg. threads; POE; IO::Select; CRLF; Unix Network Programming;

      He doesn't know what his goal is; so attempting to point him in the right direction is impossible.


    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.