2

i want to create the following:

  • 1.open a connection to a udp or tcp
  • 2.send somme data to the selected IP address
  • 3.receive the data from the selected ip address
  • 4.capture the receive data inside a variable.

The task is: the user sends a command to a device the device responds and i do something with the data received. I have the following code but i think i need a loop or a while function to wait for the received data. The user sends the command but if the response is not instantaneous i have to wait for it ..or check for it. i am using html , css and PHP here is the code i use.

 <?php

    if(isset($_POST['btn_cmd']))
    {
        $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
        socket_bind($sock, '127.0.0.1', 23);

        $msg = "AT+BC=RTIME";
        $len = strlen($msg);

        socket_sendto($sock, $msg, $len, 0, '127.0.0.1', 23);
        $from = '';
        $port = 0;
        socket_recvfrom($sock, $buf, 30, 0, $from, $port);      
 ?>

    <input type="text" 
           name="name3" 
           size="25" 
           maxlength="50" 
           value="<?php echo($buf); ?>">

 <?php
        socket_close($sock);
    }
 ?>

can u please help me with an example for the receive part i use this new code:

$out='';
        echo "Reading response:\n\n";
        //$out = socket_read($socket, 2048,PHP_NORMAL_READ) ;
        while ($out=socket_recv($socket, $buf, 2048,PHP_NORMAL_READ)) {

            echo "mesaj out:" .$out;

the problem is that i need to wait for the response and the code above is not working. i think i need a while function or something to do a looop and wait for the response and then close that loop. and i don;t know how to do that. i;ve tried all the receive and listening php functions.all i need is a small example to start from.

1 Answer 1

1

Your question is a bit broad, but the upshot is that you need to put a listener in a loop. Here is an example with an infinite loop:

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

    file_put_contents('/var/log/udp.log', "Couldn't create socket: [$errorcode] $errormsg\n", FILE_APPEND);
}

// bind the source address
if( !socket_bind($sock, $localIP, 23) )
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    file_put_contents('/var/log/udp.log', "Could not bind socket: [$errorcode] $errormsg\n", FILE_APPEND);
}

// do some communication, this loop can handle multiple clients
while(1)
{
    // receive some data
    $r = socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);
    $parsed_data = json_decode($buf); // assumes data is JSON
    $now = date('Y-m-d H:i:s');
    file_put_contents('/var/log/udp.log', "$now\t$buf\n", FILE_APPEND);

    // do stuff with JSON you received

}

 socket_close($sock);
Sign up to request clarification or add additional context in comments.

5 Comments

i have tried the exemple above . i now can send the command ! but when i try to receive it goes into an infinite loop ?
while(1) sets up an infinite loop as a listener. Any command that you send will have to be done outside of the loop or you will have to add logic to send the commands as you need them @DanielSerban
Jay .first of all thank you for your answer. i am trying to use this code: ------ $out=''; echo "Reading response:\n\n"; //$out = socket_read($socket, 2048,PHP_NORMAL_READ) ; while ($out=socket_read($socket, 2048,PHP_NORMAL_READ)) { echo "mesaj out:" .$out; } ------ but with no succes. can you please provide me with an example.
Please do not dump code in comments. Edit your original post to add any further information and do not use an answer to show updates.
sorry . please find my question above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.