Hi there's problem with the socket_read function below, it does not return empty string '' after it has finished reading the data. On second call to socket_read() it just hang there, the page keep loading in browser with no return. It works if the server close the connection, is it a must for server to close the client socket ? Thanks
$json = $_GET['json'];
/* Get the port for the WWW service. */
// $service_port = getservbyname('www', 'tcp');
$service_port = "1111";
/* Get the IP address for the target host. */
$address = "192.168.3.5";
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
die();
} else {
//echo "OK.\n";
}
//echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
die();
} else {
//echo "OK.\n";
}
$out = '';
//echo "Sending HTTP HEAD request...";
$result = socket_write($socket, $json, strlen($json));
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
die();
} else {
//echo "OK.\n";
}
echo socket_read($socket, 2048);
echo socket_read($socket, 2048);
//
// while (socket_read($socket, 2048) !== '') {
// echo $out;
//
// }
//
socket_close($socket);