简体   繁体   中英

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. Unfortunately when I use fsockopen() the the text gets all garbled with question marks and unknown characters. 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
//$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):

.....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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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