$socket = socket_create(AF_INET, SOCK_DGRAM, 0);
socket_set_option($socket,SOL_SOCKET, SO_RCVTIMEO, array("sec"=>2, "usec"=>0));
$result = socket_connect($socket, $this->ip, $this->port);
if($result <= 0) {
echo "connect() failed.\nReason: ($result) \n";
}
$data = "\xFF\xFF\xFF\xFF\x54\x53\x6F\x75\x72\x63\x65\x20\x45\x6E\x67\x69\x6E\x65\x20\x51\x75\x65\x72\x79\x00";
socket_write($socket, $data, strlen($data));
$out = socket_read($socket, 4096);
socket_close($socket);
if($out == null ){
echo 'data null';
}
else {
$queryData = explode("\x00", substr($out, 6), 5);
$server['ip'] = $this->ip;
$server['port'] = $this->port;
$server['name'] = $queryData[0];
$server['map'] = $queryData[1];
$server['game'] = $queryData[2];
$server['description'] = $queryData[3];
$packet = $queryData[4];
//$app_id = array_pop(unpack("S", substr($packet, 0, 2)));
$server['players'] = ord(substr($packet, 2, 1));
$server['playersmax'] = ord(substr($packet, 3, 1));
$server['bots'] = ord(substr($packet, 4, 1));
$server['dedicated'] = substr($packet, 5, 1);
$server['os'] = substr($packet, 6, 1);
$server['password'] = ord(substr($packet, 7, 1));
$server['vac'] = ord(substr($packet, 8, 1));
$this->info = $server;
echo "<pre>";
print_r($queryData);
echo "</pre>";
}
//error_reporting($oldErrorReporting);
此代码适用于我的旧服务器(centos + php5 + apache2); 在转移到另一台服务器(ubuntu + php5 + apache2)后,它停止了工作。
当问题不是设置php(它是在套接字支持上),但事实上他根本没有收到请求,虽然没有连接错误,但他背叛了。 可能的原因之一是认为使用外部ip访问服务套接字的问题,但我不知道如何解决它。 提前感谢您的回答!