使用PHP通过UDP发送命令消息

时间:2015-04-24 06:10:47

标签: php sockets network-programming udp

在PHP中通过UDP发送命令消息

问题陈述: 我必须通过UDP发送命令消息请求。我没有这方面的任何经验。我有这样的界面文件。

  • 向UDP端口52005发送命令消息。如果设备处于活动状态,您将收到本地UDP的响应 港口52085.

1)登录阻止请求

    SOURCE PORT = 52085
    DESTINATION PORT = 52005
    Message Data Structure
    COUNT = 1 byte message padding length 0x09
    TOKEN = 1 byte random value
    SESSION = 2 bytes random value
    COMMAND = 3 bytes command 0x43 0x00 0x00
    PADDING = 9 bytes random data

2)登录阻止响应

    SOURCE PORT = 52005
    DESTINATION PORT = 52085
    Message Data Structure
    COUNT = 1 byte
    TOKEN = 1 bytes save and use for next command
    SESSION = 2 bytes
    TYPE = 1 byte response type 0x63
    INDEX = 1 byte site receiver number
    STATUS = 1 byte receiver status
    BIT0 = network outage
    BIT1 = ping fail
    BIT2 = DTU activity
    BIT3 = host ok
    LOGIN = 1250 bytes login block
    each bit indicates a logged in account 0000 to 9999
    PADDING = 11 bytes random data`enter code here`

我所做的是:

一个。使用此代码测试IP上的UDP连接,这很好。

<?php
set_error_handler('my_error_handler');
function my_error_handler($errno, $errstr, $errfile, $errline) {}
function checkUDP($host,$port=80){
    //look no suppression
    $fp = fsockopen("udp://".$host, $port, $errno, $errstr,1.0);
    if (!$fp) {
        return false;
    } else {
        fclose($fp);
        return true;
    }
}

$good = '10.10.2.252';
if(checkUDP($good,52005)){
    echo $good.' Good';
}else{
    echo $good.' Bad';
}

?> 

湾创建了一个php文件,用于在端口52005上接收响应

<?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, "0.0.0.0" , 52085) )
{
    $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 "$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);

℃。为发送消息创建了一个php文件。

<?php
error_reporting(~E_WARNING);

$server = '10.10.2.252';
$port = 52005;

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

//Communication loop
while(1)
{
    //Take some input to send
    echo 'Enter a message to send : ';
    $input = fgets(STDIN);

    //Send the message to the server
    if( ! socket_sendto($sock, $input , strlen($input) , 0 , $server , $port))
    {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);

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

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

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

    echo "Reply : $reply";
}

但是当我发送任何消息时,我没有得到任何回复。此外,我不知道如何验证我做得对不对。 请帮我。任何小信息都可以帮到你。 感谢。

0 个答案:

没有答案
相关问题