我对套接字如何工作很新。我正处于学习过程中,但无论我搜索多少,我都无法理解。
我今天制作了这个简单的脚本:
<?php
$ip = getHostByName(getHostName()); //Get IP of machine
$address = $ip;
$port = 34242;
echo "Starting up data processing server...\n";
$socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
if($socket === false){
echo "Socket failed to connect.\n";
exit(1);
}
socket_set_nonblock($socket);
$stopped = false;
$lastread = microtime(true);
while($data = socket_read($socket, 4) && !$stopped){
$lastread = microtime(true);
}
echo("Closing data processing server, bye!\n");
?>
现在当我在我的控制台(本地主机)中运行它时,我收到错误:
Warning: socket_read(): unable to read from socket [57]: Socket is not connected in /Users/***/Desktop/sockt.php on line 22
我做错了什么?我的目的是创建一个套接字,可以将数据汇集到客户端。