in reply to Sockets

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.

Replies are listed 'Best First'.
Re: Re: Sockets
by meonkeys (Chaplain) on Apr 24, 2001 at 15:03 UTC
    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).

    I'm interested in setting up pipes as the kids are forked! What manpages/books are best for reading how to do this? Could you cook up a code snippet or direct me to one?