需要UDP客户端示例

时间:2015-05-16 05:11:16

标签: php udp

我有Sail Timer wind vane通过风向标硬件创建的WiFi网络通过UDP传输数据。系统工作正常,我可以通过3rd party INavX navigation software读取数据。但我希望能够通过PHP直接读取UDP流进行测试,我无法运行我的脚本。我以前没有UDP经验。

我知道硬件和网络运行正常,因为前面提到的软件工作正常。此外,我可以在wireshark中看到网络流量。我试着向供应商寻求帮助,但他们说他们正在逐步淘汰我的模型,他们不会帮助我使用我的模型,我应该买一个新模型。但编写导航软件的人说它是一个简单的网络连接,没有启动字符串或任何东西,"只需连接到端口,你就应该看到数据了。"

这与我见过的其他UDP示例有什么不同,这实际上是#34;只听#34; (广播是什么?)。如果我尝试将数据写入套接字,我会得到一个"权限被拒绝"错误,这就是我在socket_recvfrom函数中指定ip和port的原因。

这是我在PHP中的客户端代码:

<?php

# based on from http://www.binarytides.com/udp-socket-programming-in-php/

/*
    Simple php udp socket client
*/

//Reduce errors
error_reporting(~E_WARNING);

// network settings for the wind vane
$server = '255.255.255.255';
$port = 55554;
if( isset($argv[1]) ) $server = $argv[1];
if( isset($argv[2]) ) $port   = $argv[2];

if(!($sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)))
{
    $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" , 55554) )
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

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


//Communication loop
while(1)
{

    //Now receive reply from server and print it
    if( ($bytesRead = socket_recvfrom ( $sock , $reply , 2045 , 0 , $server, $port )) === FALSE)
    {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);

    if( ! in_array($errorcode, array(35) ) )
      die("Could not receive data: [$errorcode] $errormsg \n");
    }

    if( $bytesRead > 0 ) {
      echo "Reply : $reply";
    }
    usleep(500000);
}

2 个答案:

答案 0 :(得分:2)

尝试一下:

//Create socket.
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if (!$socket) { die("socket_create failed.\n"); }

//Set socket options.
socket_set_nonblock($socket);
socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, 1);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
if (defined('SO_REUSEPORT'))
    socket_set_option($socket, SOL_SOCKET, SO_REUSEPORT, 1);

//Bind to any address & port 55554.
if(!socket_bind($socket, '0.0.0.0', 55554))
    die("socket_bind failed.\n");

//Wait for data.
$read = array($socket); $write = NULL; $except = NULL;
while(socket_select($read, $write, $except, NULL)) {

    //Read received packets with a maximum size of 5120 bytes.
    while(is_string($data = socket_read($socket, 5120))) {
        echo $data;
    }

}

答案 1 :(得分:0)

以上情况对我没有用,总是带有无效协议的某种错误:

虽然这很好用:

C ++接收器:

unlink("/tmp/mysock");
socklen_t address_length = sizeof(struct sockaddr_un);
int_socket_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
if(_socket_fd<=0){
    perror("socket error socket()");
    return false;
}
struct sockaddr_un server_address = {0};
server_address.sun_family = AF_UNIX;
strcpy(server_address.sun_path, "/tmp/mysock");
if(bind(_socket_fd, (const struct sockaddr *) &server_address, 
        sizeof(server_address)) < 0)
{
    perror("bind ()");
    close(_socket_fd);
    _socket_fd=0;
    return false;
}
system("chmod 777 /tmp/mysock");

PHP:发件人

$sock = socket_create (AF_UNIX, SOCK_DGRAM, 0 );//
if($sock!=null)
{
    socket_sendto($sock, "ls\n", 3, 0, "/tmpmysock", 0);
    fclose($sock);
}