agh has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a program which needs to fork off a bunch of worker childen and then it needs to talk to them. I'd like to do this using IO::Socket::UNIX, but I've never done anything like that before. Does anyone have some examples they could point me to which demonstrate parent / child IPC using IO::Socket::UNIX?

Drew

  • Comment on examples of IO::Socket::UNIX usage please?

Replies are listed 'Best First'.
Re: examples of IO::Socket::UNIX usage please?
by castaway (Parson) on Nov 01, 2003 at 07:11 UTC
    Have you looked in the perl documentation? Try looking in the perlipc manpage. There are some nice simple examples on writing IO::Socket clients and servers..

    You basically want to write a server that listens to a certain port, and in each of your forked children, write an IO::Socket client which connects to that server. The server should probably also use IO::Select, which you can add a bunch of connections to, and deal with each one as and when it has data to be dealt with.

    Eg: Assuming $sock1 and $sock2 are accepted client sockets on the server, and $server is the server socket:

    my $sockets = IO::Select->new(); $sockets->add($server); $sockets->add($sock1); $sockets->add($sock2); while(1) { my @handles = $sockets->can_read(3); foreach my $handle (@handles) { if($handle == $server) { # Accept new client connection from server } elsif($handle == $sock1) { # Do something with info from $sock1 } elsif ... # etc etc. } # last when whenever.. }
    (Should probably keep the client sockets in an array though, easier to add/delete that way..)

    Hope this makes any sense.. Post some code if you get stuck..

    C.