rsegoly Posted June 29, 2013 Share Posted June 29, 2013 Hi I am newbie to PHP, I am touching it as a possible solution. I have a device at home which measures electricity consumption, it is connected to my router and also sends data using UDP to target IP and port I can specify port 9999 in my case. The data is in fixed length and structure packets, as plain text. I want to capture the data and later process it. I have Apache server on my machine at home, I can send the data to my PC and see it coming through using Netcat. But using PHP fails, and the page hung. I just copied example from PHP documentation and it supposed to be straight forward. Anyone can help? The link is for the short script I used. If it's easy I can send the data to another IP and port for someone to debug. I think this commands is the problem socket_recvfrom When I set Netcat to listen and use netstat -lpn |grep :9999 I see nothing but packets are coming through When I use PHP code as in the example I see a process listening on the port, so the behavior of Netcat and the code is different. I reached the limits of my knowledge ") Roni Quote Link to comment https://forums.phpfreaks.com/topic/279679-udp-listen/ Share on other sites More sharing options...
trq Posted June 29, 2013 Share Posted June 29, 2013 Creating a socket server inside of another server (apache) is not a good idea. You would create this as a stand alone command line script. You should probably save yourself some pain too and use a decent framework like ReactPHP. Quote Link to comment https://forums.phpfreaks.com/topic/279679-udp-listen/#findComment-1438454 Share on other sites More sharing options...
rsegoly Posted June 29, 2013 Author Share Posted June 29, 2013 but then I will draft into new adventure of learning something new How easy is it to create such server using ReactPHP? Quote Link to comment https://forums.phpfreaks.com/topic/279679-udp-listen/#findComment-1438459 Share on other sites More sharing options...
trq Posted June 29, 2013 Share Posted June 29, 2013 How easy is it to create such server using ReactPHP? Very easy. A few lines of code in fact. <?php $loop = React\EventLoop\Factory::create(); $server = new React\Socket\Server($loop); $server->on('connection', function ($conn) { $conn->on('data', function ($data) use ($conn) { echo "received $data\n"; }); }); $server->listen('127.0.0.1', 9999); $loop->run(); Quote Link to comment https://forums.phpfreaks.com/topic/279679-udp-listen/#findComment-1438583 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.