嗨,下面的socket_read函数有问题,它在读取完数据后不返回空字符串''。在第二次调用 socket_read() 时,它只是挂在那里,页面继续在浏览器中加载,没有返回。如果服务器关闭连接,它会起作用,服务器是否必须关闭客户端套接字?谢谢
$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);