Jump to content

Recommended Posts

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 

Link to comment
https://forums.phpfreaks.com/topic/279679-udp-listen/
Share on other sites

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();
Link to comment
https://forums.phpfreaks.com/topic/279679-udp-listen/#findComment-1438583
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.