Well, I've been through the perldoc and it seems to mostly focus on the normal forking server model. Having a separate process for every client is all well and nice for most things but I have a slightly different situation.

Imagine you are building a simple chat. A thousand users connecting with telnet or something primitive like that. Everything one client sends is bounced back to all connected clients. In this case, is it really a good idea to have a separate process for every client? I can't help but think it would be easier to have one single process with a list of sockets. That way you could read from all clients in turn (non-blocking) and in the case of finding a message waiting, simply print it to all the sockets.

This does however give me a problem. In order to connect new clients I must do an accept() every now and then. That is blocking - which is bad in this cas since it will block the only process until someone connects.

So, am I barking up the wrong tree here? How would you people solve something like this? Will I be forced to muck about with threads, having one that looks for clients and one that does the chatting?

-- Curious


In reply to Non-forking server? by Anonymous Monk

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.