1. Unless you're using non-blocking sockets, a read(FH) will block, or pause execution of your program, until it gets enough data to satisfy your request. So, read(FH, ..) will wait until there's data to be read, and then return that to your application. With non-blocking sockets, read() will return immediately, with or without data. It's up to you to use select() or check the return values to see if you got data, and if so, process it accordingly, keeping in mind that you may not have all of the data you need to complete your request. You might have half a line, for example, with the other half available in a few milliseconds.

2. Put in a timer (via alarm perhaps) or a counter (per transaction or per byte), killing the connection if you exceed a certain threshold. A properly designed protocol will limit the lengths of commands, and kill the connection when a bad command is seen.

3. Either construct a main() type of function to be called by the body of the application, or a one-time poll() type of function that you expect the application will call frequently. In the latter case, you could just do a select() to see if there's anything waiting, and if so, read it and process it, then return control to the application.

4. Be sure when you're doing your select processing that you go ahead and run through the entire list of waiting sockets before saying you're done. That will ensure everyone is handled.

5. That's probably more of a question about your higher-level design than anything else, and without more background about what your application is supposed to do, I can't offer any alternatives, but if your messages are small enough, even once a second is pretty trivial.

There are modules out there specifically for doing these types of things behind the scenes. NetServer::Generic I think is one of them. I'm not sure how it does with non-blocking sockets, but it's a good starting point for simple client-server types of apps. Good luck.


In reply to Re: Multiple clients with IO::Sockets by Fastolfe
in thread Multiple clients with IO::Sockets, IO::Select by swiftone

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.