I'm trying to build a forking server which uses unix domain sockets for
communication (on solaris 7).
I've run into a problem where once a connection has been established and
serviced by a child, my domain socket won't accept any more incoming
connections.

I modified my code so it doesn't fork (handles all the connections itself),
and it will service any number of connections.  As soon as I  try and have
forked children handle the connections, the socket stops listening after the
first transaction.  The server then terminates, since the accept() call
fails.

Another thing: it works fine on redhat linux, but on solaris I have the
above problem.  Is there a trick to using domain sockets in perl?  Am I
missing something?

thanks,
ross


-----------
the server:


#!/usr/local/bin/perl -w

use IO::Socket;
use POSIX ":sys_wait_h";
use strict;

my $sockname = "/tmp/unixsockd.sock";

service_clients (get_sock ());

sub get_sock
{
   unlink $sockname;
   my $sock = IO::Socket::UNIX->new (
                                     Local  => $sockname,
                                     Type   => SOCK_STREAM,
                                     Listen => SOMAXCONN,
                                    )
   or die "$0: error starting daemon on '$sockname': $@\n";
   return $sock;
}

sub service_clients
{
   my $sock = shift;
   $SIG{CHLD} = \&reaper;

   while (my $client = $sock->accept ())
   {
      next if my $pid = fork ();
      die "Cannot fork\n" unless defined $pid;

      # child
      close $sock;              # no use to child
      process_requests ($client);
      exit;                   # terminate child
   }
   continue
   {
      close $client;            # no use to parent
   }
}

sub process_requests
{
   my $client = shift;
   while (defined (my $line = <$client>))
   {
      last if $line =~ /^\s$/;     # exit on empty line
      chomp $line;
      printf $client "%s: %s, handled by PID %d\n",
         scalar localtime (time), $line, $$;
   }
}

sub reaper
{
   while (waitpid (-1,WNOHANG) > 0) {}
   $SIG{CHLD} = \&reaper;
}


---------------
and the client:

#!/usr/local/bin/perl -w

use IO::Socket;

my $sockname = "/tmp/unixsockd.sock";
my $client = IO::Socket::UNIX->new(
                                   Peer   => $sockname,
                                   Type   => SOCK_STREAM,
                                   Timeout => 5,
                                  )
   or die "$0: error connecting to '$sockname': $@\n";

my $pid = fork ();
die "Cannot fork\n" unless defined $pid;

if ($pid)
{
   write_sock ();
   waitpid ($pid, 0);
}
else
{
   read_sock ();
}

sub write_sock
{
   for (1..1000)
   {
      print $client "testline number $_\n";
   }
   print $client "\n";
   print "Done writing.\n";
}

sub read_sock
{
   print $line while (my $line = <$client>);
}


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to