使用我的浏览器从PHP读取UDP数据包

时间:2014-12-04 01:16:57

标签: php sockets

我试图制作一个php脚本来从浏览器中读取udp数据包,但我只做了一个只能用cmd with(php filename.php),我的问题是如何从浏览器中读取相同的方式得到包!

我使用的PHP脚本:

<?php

//Reduce errors
error_reporting(~E_WARNING);

//Create a UDP socket
if(!($sock = socket_create(AF_INET, SOCK_DGRAM, 0)))
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Couldn't create socket: [$errorcode] $errormsg \n");
}

echo "Socket created \n";

// Bind the source address
if( !socket_bind($sock, "127.0.0.1" , 2223) )
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Could not bind socket : [$errorcode] $errormsg \n");
}

echo "Socket bind OK \n";

//Do some communication, this loop can handle multiple clients
while(1)
{
    echo "Waiting for data ... \n";

    //Receive some data
    $r = socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);
    echo $buf;

    //echo "$remote_ip : $remote_port -- " . $buf;

    //Send back the data to the client
    //socket_sendto($sock, "OK " . $buf , 100 , 0 , $remote_ip , $remote_port);
}

socket_close($sock);
?>

0 个答案:

没有答案