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.