简体   繁体   English

从udpsocket发送和获取数据

[英]send and get data from udpsocket

I'm working on a Server list viewer and somebody told me that i should use UdpSocket to send data to the master server and receive data from it (in this case for MW2-IW4 game) 我正在使用服务器列表查看器,有人告诉我应该使用UdpSocket将数据发送到主服务器并从中接收数据(在本例中为MW2-IW4游戏)

So this is the command or the socket you use 这是您使用的命令或套接字

"\xff\xff\xff\getservers IW4 <MASTERSERVERPORTHERE> full empty\x00"

so i tried to work with this code 所以我尝试使用此代码

<?php  
  $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);

    $msg = "\xff\xff\xff\getservers IW4 <MASTERSERVERPORT> full empty\x00";
    $len = strlen($msg);

    socket_sendto($sock, $msg, $len, 0, 'MASTERSERVERIP' , 1223);
    socket_close($sock);
?>

but didn't receive any data , just getting the number 36 ... PS: 但没有收到任何数据,只是得到了数字36 ... PS:

i don't know much things about sockets.

i figured out how to do it a while ago and remembered this post, if anybody is still interested, this is how you do it: 不久前,我想出了如何做的事情,并想起了这篇文章,如果仍然有人感兴趣,这就是您的做法:

<?php
$ip = "127.0.0.1";
$port = 1337;
$socket = fsockopen('udp://'.$ip, $port);
stream_set_blocking($socket, 0);
stream_set_timeout($socket, 1); //1 = timeout

fwrite($socket, "YOUR UDP COMMAND HERE\n");

$time=time()+1; //1=timeout
$returned = "";
while($time > time()) {
    $returned .= fgets($socket);
}


echo($returned); // the returned value
@fclose($socket); //we are done, so better close it.
?>

Just use fsockopen it's a lot easier, 只需使用fsockopen,它会容易得多,

$fp = fsockopen("udp://127.0.0.1", 13, $errno, $errstr);

if (!$fp) echo "ERROR: $errno - $errstr<br />\n";

fwrite($fp, "\n");
while (!feof($fp)) {
    echo fgets($fp, 128);
}
fclose($fp);

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

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