Need help with a simple UNIX sockets server based on IO::Socket::UNIX
- From: "Craig Manley" <glideraerobatics@xxxxxxxxxxx>
- Date: 14 Apr 2005 02:02:55 -0700
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;
}
.
- Follow-Ups:
- Prev by Date: RFC: DBIx::Counter - persistent counter class
- Next by Date: Re: Need help with a simple UNIX sockets server based on IO::Socket::UNIX
- Previous by thread: RFC: DBIx::Counter - persistent counter class
- Next by thread: Re: Need help with a simple UNIX sockets server based on IO::Socket::UNIX
- Index(es):
Relevant Pages
|
|