Proc::Daemon and Sockets

S

shrike

Howdy,

I've written a TCP proxy in Perl. It is non-forking and restarts a
listening socket on the original port after a disconnection. From the
CLI it runs correctly. listen->connect->disconnect->listen.

But when I try to daemonize:

&Proc::Daemon::Init ;

The first socket will come up, but after closing it, I can not build a
socket again on the same port.

Anybody know how to fix this?

It dies at "The socket will not listen.", but only when running
daemonized.

I've included copies of my listen, accept and disconnect functions.
These use AUTOLOADed set/gets, so these examples won't build unless you
use Class::GAPI or some equivilant. I'm also using IO::poll instead of
select. In a nutshell, $self->foo("bar") is the same as $self->{'foo'}
= "bar" ; Probably won't help much, but there it is.

-Thanks
-Matt



package Proxy ;
use IO::poll qw(POLLIN POLLOUT POLLHUP POLLNVAL POLLERR) ;
use Time::HiRes qw(usleep) ;
use Socket qw(inet_aton inet_ntoa); # string to bin, bin to string
use IO::Socket::INET ;
use Class::GAPI ;
our @ISA = qw(Class::GAPI) ;

# Listen
#################
sub _createsock_listen {
my $self = shift ;

my $foo = IO::Socket::INET->new(Listen => 1,
LocalPort => $self->{'l'},
Reuse => 1,
Blocking => 0,
Proto => 'tcp')
|| die "The socket will not listen." ;

$self->_LISTEN($foo) ;

warn("Listening for a client on $foo") if ($self->{'W'}) ;
}

#Accept
#################

sub _wait_accept { # Listen to Connect
my $self = shift ;
my $waiting = 1 ;
my $foo ;

while ($waiting) {
usleep $self->{'_listendelay'} ;
next unless $foo = $self->_LISTEN->accept ;
$waiting = 0 ;
next ;
}
$foo->blocking(0) ;
$self->_handles->mask($foo => POLLIN|POLLOUT) ;
$self->_IN($foo);

warn ("Client connection accepted on $foo") if ($self->{'W'})
;
}

# disconnect
##################
sub _disco { # Reset Everything but the running flag
my $self = shift ;
$self->CONNECTED(0) ; # stop transmitting
$self->AUTHENTICATED(0) ; # kick the user
$self->_delshims() ; # Waste any shim hooks
$self->_handles->mask($self->_IN() => 0) ; # Tell poll to drop
the handles
$self->_handles->mask($self->_OUT() => 0) ; #
close($self->{'_IN'}) ; # Then close them
close($self->{'_OUT'}) ; #
close($self->{'_LISTEN'}) ; #
warn "Disconnection complete" ; #
}

1;
 
X

xhoster

Howdy,

I've written a TCP proxy in Perl. It is non-forking and restarts a
listening socket on the original port after a disconnection. From the
CLI it runs correctly. listen->connect->disconnect->listen.

But when I try to daemonize:

&Proc::Daemon::Init ;

The first socket will come up, but after closing it, I can not build a
socket again on the same port.

Anybody know how to fix this?

It dies at "The socket will not listen.", but only when running
daemonized.

Why not have Perl tell you *why* the socket will not listen? Print $! and
$@ upon failure.


Xho
 
S

shrike

A. Sinan Unur said:
I don't have time to look into this now, but, how about checking why
listen failed?

die "Listen failed:\n\$! = $!\n\$@ = $@";

Sinan
--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

Thanks for the help!

This is my error:

Listen failed: $! = Address already in use $@ = IO::Socket::INET:
Address already in use.

At first I thought it was a userid problem so I did this:

warn $> ; #
warn $< ; #
my $foo = IO::Socket::INET->new(Listen => 1,
LocalPort => $self->{'l'},
Reuse => 1,
Blocking => 0,
Proto => 'tcp')
|| die "The socket will not listen." ;

And it seems to clear the failure. Why I have no clue. I'd rather not
depend on that fix. Is there a way to more explicity clear the "Address
already in use" problem ? I'm using the latest libs for everything
except the core libraries with perl 5.8.7.

-Thanks in advance!
-Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top