我可以成功地将一些文本写入我的套接字(我知道这是因为它返回字节长度),但是当我尝试socket_read时它会返回并清空字符串。我已经尝试使用带有MSG_WAITALL参数的socket_recv,它返回0个字节。这是怎么回事? 奇怪的是,我可以收到这样的信息:
$input = "HEAD / HTTP/1.1\r\n";
$input .= "Host: mydomain.com\r\n";
$input .= "Connection: Close\r\n\r\n";
这是我的完整代码:
<?php
echo "loading socket...";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$result = socket_connect($socket, "fefansoftware.com", 80);
$input = "hello world";
echo socket_write($socket, $input, strlen($input));
$buffer = 'This is my buffer.';
if ($bytes = socket_recv($socket, $buffer, 2048, MSG_WAITALL)) {
echo $bytes . " bytes received";
echo $buffer;
}else {
echo "read failed";
}
socket_close($socket);
?>
为什么我不能收到hello world消息,但我可以收到“HEAD / HTTP1.1 ...”消息?