Mirage has asked for the wisdom of the Perl Monks concerning the following question:
Hi there,
I'm(still..)searching for a method of communicating
between forked processes in both directions, I tried
named pipes, pipes, Unix Domain Sockets but so far
I have found nothing which is really useful in my case.
I have a server which forks everytime a user connects,
then the data the child gets from the client has to get
back to the parent, but as every child does this, the
connection has to be between one end and many ends,
not as domain sockets are between one and one.
also i want to be able to read from it just for a short
time, then do other things and read again.
Or maybe someone can explain me how this 'often used'
type of servers is done, with multiple clients which send and receive
data from the server, at the same time as other clients of course,
like a chat with tcp
(I dont want to do a chat nor do i want to use udp)
So my question is: Which method is to be preferred,
and how would a chat server with tcp
be done?
Thanks in advance.
Mirage
2001-11-06 Edit by Corion : Removed superfluous blank lines
Edit: chipmunk 2001-11-06
Re: bidirectional IPC (Linux)
by traveler (Parson) on Nov 07, 2001 at 03:37 UTC
|
From your description, it sounds as though pipes would be fine. You can read them when you want and you can have many pipes: one to each child. One parent can talk to each child with its own pipe. Why do you think "but as every child does this, the
connection has to be between one end and many ends"? Do you need to send one message to all the clients at once? Are you trying to send a message from the parent and let a single client pick it up?
I'm not sure exactly what you need to do, but you can thread the server (that's what a lot of chat servers do) or you can use select(the four argument one) to decide when you need to read from a child.
If you give us some more information, or maybe an example of what you've tried, we might be able to be of more help.
HTH, --traveler | [reply] [d/l] |
|
>...can read them when you want and you can have many pipes: one to each child.
but I have to have two pipes to each child, don't I?
>Do you need to send one message to all the clients at once?
thats right, this wont work...
>I'm not sure exactly what you need to do, but you can thread the server (that's what a lot of chat servers do)
>or you can use select(the four argument one) to decide when you need to read from a child.
hmm threads will not be present in their actual form
in future releases of perl, as I have extracted from man thread, so i think thats not the best way to do it.
Select is then a method without fork, but I haven't understood it completely, what does the Select do, it
determines from which socket 'data comes in', and then
reads it?
Thanks
| [reply] |
|
but I have to have two pipes to each child, don't I?
Yes. If you want to follow the rules and if you are sending data bidirectionally. Or you can use a socket. Sockets are bidirectional and work very much like pipes once established.
thats right, this wont work...
I'm not sure what you mean by this.
Select is then a method without fork, but I haven't
understood it completely, what does the Select do, it
determines from which socket 'data comes in', and then
reads it?
No. select tells you when a filehandle is ready for reading or writing or when it has an error condition. You need to do the I/O yourself. It can be used with or without fork.
While the implementation of threads, and maybe the syntax may change, using threads may still be your best bet.
HTH, --traveler
| [reply] [d/l] |
|
Re: bidirectional IPC (Linux)
by Zaxo (Archbishop) on Nov 07, 2001 at 06:39 UTC
|
From man IO::Select:
Here is a short example which shows how `IO::Select' could be used to write
a server which communicates with several sockets while also listening for
more connections on a listen socket
use IO::Select;
use IO::Socket;
$lsn = new IO::Socket::INET(Listen => 1, LocalPort => 8080)
+;
$sel = new IO::Select( $lsn );
while(@ready = $sel->can_read) {
foreach $fh (@ready) {
if($fh == $lsn) {
# Create a new socket
$new = $lsn->accept;
$sel->add($new);
}
else {
# Process socket
# Maybe we have finished with the socket
$sel->remove($fh);
$fh->close;
}
}
}
After Compline, Zaxo
| [reply] [d/l] |
Re: bidirectional IPC (Linux)
by Rex(Wrecks) (Curate) on Nov 07, 2001 at 05:13 UTC
|
If you don't like pipes there are other solutions, one is shared memory. tie a hash into shared memory using IPC::Shareable and used that.
Also looking into Network Programming with Perl would be a good idea. It's a really good book and has several good examples of TCP Servers including a chat server.
"Nothing is sure but death and taxes" I say combine the two and its death to all taxes! | [reply] [d/l] |
Re: bidirectional IPC (Linux)
by jepri (Parson) on Nov 07, 2001 at 14:49 UTC
|
| [reply] |
|
thanks, I will look it up later and reply or msg if I have
problems(hopefully this is what I was looking for)
| [reply] |
|
After looking at your module i think its of no real use
for me, nor for any other real server application, or
forking application, because having one one port for each process is too much, and why don't you use unix-domain-sockets?
| [reply] |
|
| [reply] |
|
|
|