Need help with a simple UNIX sockets server based on IO::Socket::UNIX



Hi. I've tried to create a simple client + server that communicate
through a unix socket.

As with all socket servers, it has a loop where it waits for
connections:

while ($client = $sock->accept()) {
# handle client here
}

The problem is that $sock->accept() is returning undef on each
alternate client connection with error "No child processes".

I made this temporary workaround that does work but of course I must be
screwing up somewhere else:

while ($client = $sock->accept() || $client = $sock->accept()) {
# handle client here
}

I hope someone can help or provide an example. I've included the client
+ server code below for those who are interrested in taking a peek.

Thanks,
Craig Manley

############ client ############
#!/usr/bin/perl -w
use strict;
use IO::Socket;

my $sockname = 'mysocket';

my $client = IO::Socket::UNIX->new('Peer' => $sockname,
'Type' => SOCK_STREAM,
'Timeout' => 50) or die "$0: error
connecting to '$sockname': $ [at] \n";
my $pid = fork();
unless(defined($pid)) {
die("Fork this! I cannot forking fork!\n");
}
if ($pid) {
write_sock();
waitpid($pid, 0);
}
else {
read_sock();
}

sub write_sock {
for (1..10) {
print $client "testline number $_\n"; # print to socket
}
print $client "\n"; # empty line causes server to terminate
connection
print "Done writing.\n"; # (goes to stdout, not socket)
}

sub read_sock {
while (my $line = <$client>) {
print $line; # report to stdout
# simulate someone reading slooowly (50ms/line):
select(undef, undef, undef, 0.05);
}
}




########### server ############
#!/usr/bin/perl -w
use strict;
use IO::Socket;
use POSIX ":sys_wait_h"; # (for WNOHANG)
use Data::Dumper qw(Dumper);

# example using unix domain socks
# once the file is created as a socket, any client can
# interact with it

my $sockname = 'mysocket';


service_clients( get_sock() ); # wait for incoming requests


sub get_sock {
unlink $sockname;
my $sock = IO::Socket::UNIX->new('Local' => $sockname,
'Type' => SOCK_STREAM,
'Listen' => SOMAXCONN) || die "$0:
error starting daemon on '$sockname': $ [at] \n";
# you might want to change permissions and ownership, e.g.:
#chmod 0600, $sockname;
#chown scalar getpwnam('nobody'), 0, $sockname;
return $sock;
}

sub service_clients {
my $sock = shift;
$SIG{CHLD} = \&reaper;
my $client;
while ($client = $sock->accept()) { # Why the hell does it return
undef on the next iteration?
# while ($client = $sock->accept() || $client = $sock->accept()) { #
This strangely enough does work.

# fork yet another process to prevent buffer deadlock. one proc
writes to
# the sock, the other reads the deamons response
my $pid = fork();
unless(defined($pid)) {
die("Fork this! I cannot forking fork!\n");
}
if ($pid) { # parent
print "$pid $$: I'm the parent and am going to wait for another
client.\n";
close($client); # no use to parent
next; # be ready for another client
}
print "$pid $$: I'm the child and I'm going to service the
client.\n";
# child
close($sock); # no use to child
process_requests($client);
print "$pid $$: I'm the child and I'm going to exit.\n";
exit; # terminate child
}
print "Damnit! I'm the server and I finished unexpectedly: $!\n";
}

sub process_requests {
my $client = shift;
$0 = "unixsockd: handling requests...";
# read from client until empty line which causes it to close
connection
while ( my $line = <$client> ) { # read line from socket
if ($line =~ /^\s$/) {
last; # exit on empty line
}
chomp($line);
# put some more useful code here to read each line or whatever...
printf $client "%s: %s, handled by PID %d\n",
scalar localtime(time), $line, $$;
# return something to client
}
}

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

.



Relevant Pages

  • Chat client/server print failed
    ... This is a chat client wrote in perl Gtk2. ... is the print statement in the send_msg_all sub. ... leave the server running for testing purposes. ... # just read means there is a complete request waiting ...
    (comp.lang.perl.misc)
  • Re: Chat client/server print failed
    ... is the print statement in the send_msg_all sub. ... leave the server running for testing purposes. ... # This would be the end of file, so close the client ... # just read means there is a complete request waiting ...
    (comp.lang.perl.misc)
  • Re: What doesnt lend itself to OO?
    ... >> proxy and instructs the server to constuct the real object. ... rather than client code. ... If 'clock' is instantiated in the server, ... > for the server interface at the OOA level. ...
    (comp.object)
  • This is going straight to the pool room
    ... or not the client has privilege to do what they're trying to do, ... The server environment is this: ... 3GL User action Routines that Tier3 will execute on your behalf during the ... Routine Name: USER_INIT ...
    (comp.os.vms)
  • Re: client close the socket. how the server knows?
    ... the server know that the socket is closed from the client side. ... > Private Delegate Sub UpdateListBoxDel(ByVal Data As String) ... > Private Sub Button1_Click(ByVal sender As System.Object, ...
    (microsoft.public.dotnet.languages.vb)