If you want to go with a forking model, the goal is for each thread to handle communications to/from the client. This way each thread has full state information about its individual client. For that reason, you probably want to set up some pipes as you fork off your children, and have the parent intercommunicate with the children using those pipes (instead of sending directly to the socket).
Basically let the parent manage the children, let it pass information between the children, and let the children do everything related to communicating with the clients.
The thing is, if you're forking already, do you really need non-blocking sockets? (Note: Your code isn't using non-blocking sockets as it is now.) In any event, when you're doing real-time interaction like this, you want to turn off buffering:
$|=1;
# and perhaps after your accept() call:
$new_sock->autoflush;
Once you get this working, you might consider trying to re-write it using
IO::Select, using non-blocking sockets, via something like this:
my $res = $sock->fcntl(F_GETFL, 0);
$sock->fcntl(F_SETFL, $res | O_NONBLOCK);
Getting something fully robust is going to take a lot more coding than you're doing now. You'll learn a lot in the progress, however. Good luck.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.