简体   繁体   中英

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)

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:

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,

$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);

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