Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^8: Does IO::Select work? Anywhere?

by BrowserUk (Pope)
on Oct 23, 2012 at 10:47 UTC ( #1000465=note: print w/ replies, xml ) Need Help??


in reply to Re^7: Does IO::Select work? Anywhere?
in thread Does IO::Select work? Anywhere?

What do you have to do to make your symptoms appear using this script?

Okay. I'll play :)

  1. Start your server in one session.
  2. Run this in a second session:
    perl -MIO::Socket -E ' $s=IO::Socket::INET->new("localhost:1200"); $s- +>send( "a" ); sleep 1e6 '
  3. Now start a third session and telnet into your server.

    You'll get a connection and be able to type stuff; but you'll get no replies and none of your telnet input will be displayed on your server's console.

  4. Start as many more clients -- telnet or otherwise -- as you like.

    Your server will never see anything from any of them until you kill that perl one-liner. (Or you wait the 11.57 hours DAYS for the sleep to time out.)

The reason is that the send('a') caused select to return a readable file handle. Your server then attempted a readline from that client; but the client never sends a newline, so the readline never returns and your server is dead in the water. The tcpip stack will service connections, but your server will never loop back to accept them.

One bad client and your server is DOS'd. This is the exact scenario that I described above with that "teaching material"; the source of my "obsession with readline".

You know I'm sure, that multiplexing with IO::Select is only meant for use with short messages; if you are doing large data transfers, you need to use fork or threads.

Absolutely not!

If you use recv (or sysread), thus avoiding line-based and buffered IO, you can service huge data packets and small ones; you simply accumulate partial packets in buffers and only process input once you've accumulated enough to satisfy the comms protocol requirements. Whether that is newline (or other character sequence) terminated records or length pre-fixed; or any other mechanism.

(Oh. And don't forget to set the sockets non-blocking!)


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
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.

RIP Neil Armstrong


Comment on Re^8: Does IO::Select work? Anywhere?
Download Code
Re^9: Does IO::Select work? Anywhere?
by zentara (Archbishop) on Oct 23, 2012 at 14:23 UTC
    Ok, I will put my mind to it. I followed your instructions and yes, it does as you say block. My first thought is isn't it clever the way the MIComplex designed our socket connections, so that they can jam any socket based system they want? ;-)

    Even though you denigrated the earlier good professor's code of sysreading 1 byte at a time, in Re^4: Does IO::Select work? Anywhere?

    while ($c ne "\n" && ! $endoffile) { if (sysread($filehandle, $c, 1) > 0) { $retstr = $retstr . $c; } else { $endoffile=1; } }
    maybe therin lies your answer?

    The intuitive side of my brain points you to IPC3 buffer limit problem, where you can use syscntl statements. Merlyn says it's effective, except for one case. :-) So I figure, what are the odds? The odds that the last buffer is exactly 4k? 1 in 4k ? Pretty good odds. :-) But then again, I don't fly spaceships either. :)

    P.S. I tried your hanging script on the server in Glib based forking server with root messaging and the server handled it just fine. It continued reading messages from clients, and accepted new clients. Sometimes, you just have to learn to use modules instead of reinventing the conveyor belt.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Even though you denigrated the earlier good professor's code of sysreading 1 byte at a time, in Re^4: Does IO::Select work? Anywhere? maybe therin lies your answer?

      No, It doesn't. Plug that into your server and replace the call to perl's readline with a call to that -- and exactly the same thing will happen. Which is why I denigrated it.

      If you're happy with not understanding why glib works, that's your call.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      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.

      RIP Neil Armstrong

        if you're happy with not understanding why glib works, that's your call.

        Oh I don't understand how most modules do what they actually do, thats why I use them. For what it's worth, the code you are probably looking for is in gio/gsocket.c

        For me to guess at how to do it with plain select, I think you need to figure out how to access the socket's errno. See getting errno. From Perl you probably need to use sysctl.ph

        P.S. Glib is not the only lib out there, many prefer AnyEvent. Have you looked at how AnyEvent works against your socket blocker program?


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1000465]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2014-09-01 20:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (17 votes), past polls