Link to home
Start Free TrialLog in
Avatar of Massimiliano Forestiero
Massimiliano Forestiero

asked on

how can i print data coming from an udp server in php?

hi..my name is massimiliano...i need to make and app in PHP that recives udp packet (form a third part application) and send it to another php or html file that print the data send in real time.

i create the server that it should receive an udp message and extract the content into the variable $output
<?php
             
       set_time_limit(0);   
        $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
        socket_bind($socket, "127.0.0.1", 16516);    
    
        for( ; ; ){
               socket_recvfrom($socket, $packet, 1500, 0, $from, $port);
               $output = "MSG Ricevuto : $packet "; 
               echo "$output " ;
               
                socket_sendto($fp1, $output, strlen ($output), 0, $from, $port);
               //i think taht whit socket_sendto(...) i can send the message to my other file
        }
       // close socket
        socket_close($socket);
?>

Open in new window

i try to make this file to print the data but it does not work..
<?php

 $fp1 = fsockopen("udp://127.0.0.1", 16516, $errno, $errstr);

 if (!$fp1) {
           echo "ERROR: $errno - $errstr<br />\n";
        }else{

        socket_recvfrom($fp1, $packet, 1500, 0, $from, $port);
        echo "stampa : $packet";
 }
?>

Open in new window


someone can help me to make the part that print the data on browser? i need that the data are update when arrive a udp packet from the server...my thirs part app send packet UDP every second

thaks to everyone

Massimiliano
Avatar of David Favor
David Favor
Flag of United States of America image

https://www.binarytides.com/udp-socket-programming-in-php/ is a good starting point.

You have no error checking anywhere, so you'll have no clue what's failing.

First thing to do, add error checking of every call (see above link for how to do this).

Likely printing error messages will tell you exactly where problem is occurring.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.