TCP/UDP Question

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?
 
B

Brian McCauley

Snail said:
Re: TCP/UDP Question

The word "question" can, and usually should, be omitted in subject lines.

I don't think your question is related to TCP/UDP.

I've been playing around with the following example code:
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 fork()! Very significantly fork().
My question is (mainly for TCP servers),

So why did you show us the code for UDP server that has nothing to do
with your problem?
if you fork the server, how can
you then communicate between multiple connected clients?

Right, that is your question. You want to know how can you communicate
between forked processes.

The usual suspects are:
Shared memory.
Temporary files.
Sockets.

If communication needs not be fast then temporary files are probably
simplest. There are also various modules on CPAN to allow you to tie
variables to shared memory or files so that you can simpl set the value
in on process and read it in another.
 
S

Snail

Jim said:
I've been playing around with the following example code:

[udp code snipped]
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?

You normally fork the server to deal with one client on a short-term
(query-response) basis. The client connects and either supplies data,
asks for data, or both. The server responds and breaks the connection.
The server would fork if responding takes awhile and it needs to deal
with other clients in the meantime.

Clients do not normally communicate with each other, unless the server
is explicitly designed to allow that service (e.g., a messaging system
or a publish-and-subscribe server). In that case, the server does not
fork, but keeps a set of connected clients. The server can then send
and receive data from/to each client independently.

So would using select (IO::Select, to go with IO::Socket::INET) be a
good solution for such a server (say, a simple chat server) ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top