$gS=new getService();
//61.152.167.18:5525
$gS->init("61.152.167.18:5525",10);
$gS->_connect();
$gS->_writeData("info");
$gS->_readData();
$gS->_disconnect();class getService{
var $errno=5;
var $errstr="error";
function init($str,$_timeout){
$arr=explode(':',$str);
$this->_ip=$arr[0];
$this->_port=$arr[1];
$this->_timeout=$_timeout;
}
function _connect(){
if (!$this->_socket = @fsockopen("udp://".$this->_ip, $this->_port, $errno, $errstr, $this->_timeout)){
return false;
}else{
return true;
}
}

function _disconnect()
{
if (!fclose($this->_socket)) return false;
return true;
}
//这里就是关键部分,hlds的指令是以\xFF\xFF\xFF\xFF,四个255的控制指令开始,以\x00一个空指令结尾,其中的command可以是info、ping、players、rules、details等等,分别得到不同的信息。
function _writeData($command){
if (!fwrite($this->_socket, "\xFF\xFF\xFF\xFF" . $command . "\x00")){
return false;
}else{
return true;
}
}
function _readData()
{
socket_set_timeout($this->_socket, $this->_timeout);
$data = fread($this->_socket, 8000);
echo ord($data);
die;
//if (socket_timeout($this->_socket)) return false;
switch (ord($data)) {
case 255:
$status = socket_get_status($this->_socket);
socket_set_timeout($this->_socket, $this->_timeout);
$data .= fread($this->_socket, $status["unread_bytes"]);
//if (socket_timeout($this->_socket)) return false;
break;
case 254:
$status = socket_get_status($this->_socket);
socket_set_timeout($this->_socket, $this->_timeout);
fread($this->_socket, 7);
//if (socket_timeout($this->_socket)) return false;
socket_set_timeout($this->_socket, $this->_timeout);
$data = fread($this->_socket, 1);
//if (socket_timeout($this->_socket)) return false;
$bits = sprintf("%08b",ord($data));
$count = bindec(substr($bits, -4));
$x = bindec(substr($bits, 0, 4));
$status = socket_get_status($this->_socket);
socket_set_timeout($this->_socket, $this->_timeout);
$datagrams[$x] = fread($this->_socket, $status["unread_bytes"]);
//if (socket_timeout($this->_socket)) return false;
for ($i=1; $i< $count; $i++) {
socket_set_timeout($this->_socket, $this->_timeout);
fread($this->_socket, 8);
//if (socket_timeout($this->_socket)) return false;
socket_set_timeout($this->_socket, $this->_timeout);
$data = fread($this->_socket, 1);
//if (socket_timeout($this->_socket)) return false;
$x = bindec(substr(sprintf("%08b",ord($data)), 0, 4));
$status = socket_get_status($this->_socket);
socket_set_timeout($this->_socket, $this->_timeout);
$datagrams[$x] = fread($this->_socket, $status["unread_bytes"]);
//if (socket_timeout($this->_socket)) return false;
}
$data = "";
for ($i=0; $i< $count; $i++) {
$data .= $datagrams[$i];
}
break;
default:
break;
}
echo $data;
//return true;
}}ps:代码运行正常,如果换一个IP的话就提示超时,希望得到高手解答
测试IP:61.152.167.18:5525[连接成功]
        220.189.241.149:27015[连接成功]
        218.16.122.180:27011[timeout]
        222.73.219.173:27023[timeout]

解决方案 »

  1.   

    你试ping下ip,  看不看是ip本身超时问题先, 再跟帖
      

  2.   

    我帮你ping了下, 发现有些ip超时, 不知我网络配置原因, 还是本身和那些ip有关
      

  3.   

    第三个就ping的通,也获取不到相应信息。不是这个问题
      

  4.   


    执行php脚本,四个ip结果分别是255 255 0 0直接ping除了第一个不超时, 其它都通
      

  5.   

    上面改正,直接ping除了第一个超时, 其它都通
      

  6.   

    61.152.167.18:5525[连接成功] 
    220.189.241.149:27015[连接成功]这两个IP我可以获取的到信息的  PING不PING的通那就和这个没关系咯
      

  7.   

    set_time_limit(0);
    文件头加了也是于世无补,为什么有些IP可以,而有些IP不行呢 ,是不是跟他游戏的服务器限制了呢?如果是这样怎样绕过呢?
      

  8.   

    你确定是使用UDP协议发送信息吗?
    我认为是服务器的问题