Hi all
php sockets are killing me. I think they might be broken.
I am using the class.jabber.php class, which uses fsocketopen to connect to a jabber server. Basically, I am finding it is impossible to detect when the connection is lost off the file handle.
the jabber class abstracts the connector functionality out, so I implemented a simple connector using the low level socket functions. the code is below.
Basically, I am finding that I cannot detect a lost connection here either. What is even wierder, is that the socket_last_error method reports an error even before I kill the server(we need reconnect functionality), and when messages are being sent fine.
Has anyone run into this before?
function OpenSocket($server, $port)
{
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($this->socket < 0)
{
echo "socket_create() failed: reason: " . socket_strerror($this->socket) . "\n";
return false;
}
$result = socket_connect($this->socket, $server, $port);
if ($result < 0)
{
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
return false;
}
// socket_set_blocking($this->socket, 0);
return true;
}
function CloseSocket()
{
return socket_close($this->socket);
}
function WriteToSocket($data)
{
echo "\nIn WriteToSocket: $data\n";
echo "-------------Socket = " . $this->socket . "\n";
if(socket_last_error($this->socket))
{
print "Detected error\n";
echo socket_strerror(socket_last_error($this->socket));
}
return socket_write($this->socket, $data, strlen($data));
}
function ReadFromSocket($size)
{
echo "In ReadFromSocket\n";
if(socket_last_error($this->socket))
{
print "Detected error\n";
echo socket_strerror(socket_last_error($this->socket));
}
set_magic_quotes_runtime(0);
unset($buffer);
$buffer = socket_read($this->socket,$size);
set_magic_quotes_runtime(get_magic_quotes_gpc());
if(substr($buffer,-1)=="\n") { echo "found end\n\n"; }
printf("%s\n", $buffer);
return $buffer;
}