S
Snail
I've been playing around with the following example code:
----------------------------------------------------------------------
#!/usr/bin/perl -w
use strict;
use IO::Socket;
$| = 1;
my $msgin;
my $listenport = 9999;
my $maxlen = 5000;
my $port = shift || $listenport;
$SIG{'INT'} = sub {exit 0};
my $sock = IO::Socket::INET->new(Proto=>'udp',LocalPort=>$port) or die
"Cannot open socket!";
while( $sock -> recv ( $msgin, $maxlen ) ) {
next if fork;
do_stuff_with( $msgin );
}
----------------------------------------------------------------------
And similar examples for tcp using accept().
My question is (mainly for TCP servers), if you fork the server, how can
you then communicate between multiple connected clients?
Seems to that, say if one client conencts, then at soem point issues a
command like "view all" there seems to be no way to itterate through the
clients to send backa reponse. In other words, client A is forked, data
is recieved from it, but since it is forked, it wont know of the other
clients. Even if the server's process (the original/main process)
maintains a list of all clients, when a client is forked, and then other
clients connect, then the version of the list the forked client "sees"
will be not be updated as new clients connect (or if old ones
disconenct.)
What is a good solution for this?
----------------------------------------------------------------------
#!/usr/bin/perl -w
use strict;
use IO::Socket;
$| = 1;
my $msgin;
my $listenport = 9999;
my $maxlen = 5000;
my $port = shift || $listenport;
$SIG{'INT'} = sub {exit 0};
my $sock = IO::Socket::INET->new(Proto=>'udp',LocalPort=>$port) or die
"Cannot open socket!";
while( $sock -> recv ( $msgin, $maxlen ) ) {
next if fork;
do_stuff_with( $msgin );
}
----------------------------------------------------------------------
And similar examples for tcp using accept().
My question is (mainly for TCP servers), if you fork the server, how can
you then communicate between multiple connected clients?
Seems to that, say if one client conencts, then at soem point issues a
command like "view all" there seems to be no way to itterate through the
clients to send backa reponse. In other words, client A is forked, data
is recieved from it, but since it is forked, it wont know of the other
clients. Even if the server's process (the original/main process)
maintains a list of all clients, when a client is forked, and then other
clients connect, then the version of the list the forked client "sees"
will be not be updated as new clients connect (or if old ones
disconenct.)
What is a good solution for this?