in reply to Two-Part Socket Question.

First off thank you guys. I have gotten soemthing from each of your replys.

Now I have another interesting issue, although it makes since I cannot figure where to put a portion of my write code to allow proper functionality.

#!/usr/bin/perl -w use strict; use IO::Socket; use IO::Select; $| = 1; # Make program hot! my $sock = IO::Socket::INET->new(Proto => 'tcp', LocalAddr => '192.168.0.100', LocalPort => 7778, Broadcast => 1, Blocking => 0, Reuse => 1, Listen => 1) or die "Can't bind : $@\n"; my $sel = new IO::Select($sock, undef, undef, 0); print "Server now listening for connections!\n" if ($sock); while(my @queues = $sel->can_read()) { my @canWrite = $sel->can_write(); foreach my $obj (@queues) { my $new; if($obj == $sock) { # Create a new socket $new = $sock->accept; $sel->add($new); }else { # Process socket if (my $pid = fork()) { # parent: close the connection so we can # keep listening $sel->remove($obj); $obj->close(); }else { # child: deal with connection while ($obj) { if (defined($obj->recv(my $data, 1024))) { last if (($data =~ /^quit/i) || ($data eq "")); print STDOUT "Input detected is: $data\n"; foreach my $target (@canWrite) { $target->send($data) if ($target);; } }else{ last; } } # finished with the socket $sel->remove($obj); $obj->close; } } } }
Now in the above server code each time a new connection comes in it is forked along with current circuit data such as already existing sockets that are both readable and writable. Now every new socket that comes in gets a more and more complete array list of sockets and this list does not match across all connection. For example, I ran this with 4 clients and the first cleint launched can only talk to the server and itself. The second one can talk to the server, first one, and itself. The third one can talk to the server, first, second, and itself. Finally the fourth can talk to the server, first, second, third, and itself.

I say this makes since because when the first connection comes in the second, third and foruth do not exist yet so the forked process has no way of knowing abou them, Same with the second, third, fourth and so-on.

How can I logically setup this code to keep an updated list of socket conenction on all forked children?


www.perlskripts.com

Replies are listed 'Best First'.
Re^2: Two-Part Socket Question.
by sgifford (Prior) on Aug 31, 2004 at 16:56 UTC

    There's not a straightforward way to do that. File descriptors are shared between parent and child, but not between siblings, so even if two sibling processes knew the file descriptor number they wouldn't be able to write to it.

    The way that's closest to what you want is probably the most complicated way. If each parent and child maintain a socket between them, the parent can send new file descriptors to all children with ioctl.

    A more straightforward way to handle it would be to have the parent process act as a central point for distributing information; it receives messages from each client over a pipe/socket, then writes them out to all of the other clients.

    Another possibility is to write messages that should be sent into a shared memory segment, created with mmap or shmget.

    Yet another possibility is to create threads instead of processes, since all threads in a process share file descriptors (though you should probably use locking to make sure writes to file descriptors don't get intermingled).

Re^2: Two-Part Socket Question.
by zentara (Cardinal) on Aug 31, 2004 at 14:42 UTC
    "I say this makes since because when the first connection comes in the second, third and foruth do not exist yet so the forked process has no way of knowing abou them, Same with the second, third, fourth and so-on. How can I logically setup this code to keep an updated list of socket conenction on all forked children? "

    It gets pretty hard to do this in a forking server model. I use the Net::EasyTCP module for this, but it dosn't use a forked model. The basic idea is to save your client objects in a hash, like this PSEUDOCODE:

    my $new; my $counter = 0; my %clients; if($obj == $sock) { # Create a new socket $new = $sock->accept; $sel->add($new); $counter++ $clients{$counter}{'obj'} = $obj; $clients{$counter}{'ip'} = $obj->remoteip; #or whatever identifiers you can get

    Now you have to figure out how to get that hash data to all your forked processes. You can either you pipes or sockets for the interprocess communication. It's more complicated than I can code of the top of my head.

    The other option is to use a threaded server model, which has been done nicely at this node: Re: Re: ChatServer

    In threads, you can use the cond_broadcast to communicate between threads.


    I'm not really a human, but I play one on earth. flash japh