简体   繁体   English

PHP fsockopen()奇怪的结果字符编码

[英]php fsockopen() strange results character encoding

I'm trying to connect to a game server to get the status of the server. 我正在尝试连接到游戏服务器以获取服务器的状态。 Looking at the connection with Wireshark things should work. 看一下与Wireshark的连接,一切应该正常。 Unfortunately when I use fsockopen() the the text gets all garbled with question marks and unknown characters. 不幸的是,当我使用fsockopen()时,文本会出现所有带有问号和未知字符的乱码。 In places the text is ok so I know I'm connecting and getting some data. 在某些地方文本没问题,所以我知道我正在连接并获取一些数据。

What should be my next step to get this working? 要使此工作正常,下一步应该怎么做? I'm thinking maybe a buffer or something like that but it's really only one packet I am receiving. 我在想也许是缓冲区之类的东西,但实际上我只收到一个数据包。

PHP code: PHP代码:

    <?php
//$fp = fsockopen("udp://173.199.102.29", 3074, $errno, $errstr);
$fp = fsockopen("udp://209.247.83.157", 3074, $errno, $errstr);
if (!$fp) {
    echo "ERROR: $errno - $errstr<br />\n";
} else {
    fwrite($fp, "\xff\xff\xff\xff\x00 playerlist\x00");
    stream_set_timeout($fp, 4);
    stream_set_blocking($fp, 1);
    $s="";
     do {
                $read = fread($fp,1024);
                        //$s .= $read;
                        $s .= $read;
                        $info = stream_get_meta_data($fp);
                }
                while (!$info["timed_out"]);
echo $s;
    fclose($fp);
}
?>

results: 结果:

-ÿÿÿÿ ����������������������������������������������������������������������������������������������������������������������������������������� á������������������������������������������������������������˜Ÿw´à�†w3†w׬«I��� á�­‡w����������������˜Ÿw��˜wtà�‹���å�ÝŸwÜà�Š–��������Øà�ª–�á�Ø>«�à/ã–�[ã�ûÿÿÿlã�@+–�����@(Œ@(ŒÌ+–�n���/$–��������Ì+–�?‹w����Ø>«�à/Ð/x,wã�������������Ë¢���������������������ä����[ã�ûÿÿÿ���@�������«æ�����?����������������������������"B��ÿÿ��µ…_�#�\pæ�+��������uží?�����:¯÷¿���������€���Ð��������Àñ}šw™”Ÿwòßšw¨”Ÿw㨫I*���*���û������+���S���+���+����¸¸�¸�������Tä�¤ä�'·)w#�����Tä�+���˜`«(ˆ`«(°ç�k��¸)w¦(šw¬ä�ÿÿ��+���S�ž+�ý~+���������������������������Àä�ãb,w#�����´ä�+���������ÀñÀ�����������������������������€@���������øÿÿÿ›À��������������������������������������������������������������������������������������������������������€¿�������������������������������56227��������k]D’°ã��g•�ã�Ë¢�����ìã�@(Œ@���%WÇS(Œ,���@(ŒB���%WÇG(Œ8���@(ŒÌã�@g•�@(Œ@���Ë¢�����ìã�����ôi�@(Œ@���Ë¢�ìã�FP_�Ë¢�F���N���Ø���ç���£Û�����¾\’úS^�

Contents of packet I should be receiving (From Wireshark): 我应该接收的数据包内容(来自Wireshark):

.....M.ML. .U\protocol\1044\clients\15\sv_maxclients\18\pure\1\hc\1\hw\2\mod\0\voice\1\pb\1\bots\0\licensetype\2\wager\0\geolocation\SEA\playlist\10......M.ML. .U.....M.ML. .U\protocol\1044\clients\15\sv_maxclients\18\pure\1\hc\1\hw\2\mod\0\voice\1\pb\1\bots\0\licensetype\2\wager\0\geolocation\SEA\playlist\10

This doesn't directly address the character mangling issue, but your code loops until it times out - even when there's no more data being sent by the server. 这不能直接解决字符重整问题,但是您的代码会循环直到超时(即使服务器不再发送任何数据)。 Use this instead: 使用此代替:

$s='';
$info=stream_get_meta_data($fp);
while (!feof($fp) && !$info['timed_out']) {
    $s.=fgets($fp,1024);
    $info=stream_get_meta_data($fp);
}
echo $s;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM