On 3/22/07, Tom Phoenix <[EMAIL PROTECTED]> wrote:
On 3/22/07, Andy Greenwood <[EMAIL PROTECTED]> wrote:
> I am getting the following error whenever I try to send data to a unix
> domain socket. PHP sends the command just fine, but perl dies as soon
> as it reads from the socket.
Are you using PHP, or Perl? Both?
Both. perl acts as a daemon and php the interface
here's the php code. I don't belive this is buggy, since it gives no
errors, but I haven't ruled it completely out.
----------begin php------------
function instance_sendCommand($command, $read = 0) {
if ($this->state == FLUXD_STATE_RUNNING) {
// create socket
$socket = -1;
$socket = @socket_create(AF_UNIX, SOCK_STREAM, 0);
if ($socket < 0) {
array_push($this->messages , "socket_create() failed:
reason: "[EMAIL PROTECTED]($socket));
$this->state = FLUXD_STATE_ERROR;
return null;
}
//timeout after n seconds
@socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO,
array('sec' => $this->_socketTimeout, 'usec' => 0));
// connect
$result = -1;
$result = @socket_connect($socket, $this->_pathSocket);
if ($result < 0) {
array_push($this->messages , "socket_connect() failed:
reason: "[EMAIL PROTECTED]($result));
$this->state = FLUXD_STATE_ERROR;
return null;
}
// write command
@socket_write($socket, $command."\n");
// read retval
$return = "";
if ($read != 0) {
do {
// read data
$data = @socket_read($socket,
4096, PHP_BINARY_READ);
$return .= $data;
} while (isset($data) && ($data != ""));
}
// close socket
@socket_close($socket);
// return
return $return;
} else { // fluxd not running
return null;
}
}
------------end php------------
here's the perl code which creates and reads the sockets
------------------begin perl-----------
sub socketInitialize {
$SERVER = IO::Socket::UNIX->new(
Type => IO::Socket::UNIX->SOCK_STREAM,
Local => $PATH_SOCKET,
Listen => 16,
Reuse => 1,
);
# check socket
unless ($SERVER) {
printError("CORE", "could not create socket: ".$!."\n");
exit;
}
# print
if ($LOGLEVEL > 0) {
printMessage("CORE", "created socket ".$PATH_SOCKET."\n");
}
# create select
$Select = new IO::Select();
# Add our server socket to the select read set.
$Select->add($SERVER);
}
....
sub checkConnections {
# Get the readable handles. timeout is 0, only process stuff that can be
# read NOW.
my $return = "";
my @ready = $Select->can_read(0);
foreach my $socket (@ready) {
if ($socket == $SERVER) {
my $new = $socket->accept();
$Select->add($new);
} else {
my $buf = "";
my $char = getc($socket);
while ((defined($char)) && ($char ne "\n")) {
$buf .= $char;
$char = getc($socket);
}
$return = processRequest($buf);
$socket->send($return);
$Select->remove($socket);
close($socket);
}
}
}
-----------end perl--------------
socketinitialize is called during daemon startup, and checkconnections
is called in a loop
> send: Cannot determine peer address at myscript.pl line 1256
You say that perl dies when it reads, but that message mentions
'send'. What are you really doing around line 1256?
line 1256 is in checkconnections: $socket->send($return);
(Why didn't you
include some of that relevant code in the first place?)
Sorry, I'm at work now, and typed that up in haste. :D
Did your perl binary pass all tests before installation?
Yup
Good luck tracking down this bug!
thanks!
--Tom Phoenix
Stonehenge Perl Training
--
--
I'm nerdy in the extreme and whiter than sour cream
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/