I have a Sail Timer wind vane that transmits data via UDP over a WiFi network created by the wind vane hardware. The system works fine, and I can read the data via 3rd party INavX navigation software. But I want to be able to read the UDP stream directly via PHP for testing, and I can't get my script to run. I have no prior experience with UDP.
I know the hardware and network are work properly because the aforementioned software works fine. Also, I can see the network traffic in wireshark. I tried asking the vendor for help, but they said they are phasing my model out, they won't help me with the model I have, and I should buy a new one. But the guy who wrote the navigation software said it's a simple network connection with no initiation strings or anything, "Just connect to the port and you should see data."
What makes this a bit different from other UDP examples I've seen is this is essentially "listen only" (is that what broadcasting is?). If I try to write data to the socket, I get a "permission denied" error, which is why I'm specifying the ip and port in the socket_recvfrom function.
Here's my client code in PHP:
<?php
# based on from http://www.binarytides.com/udp-socket-programming-in-php/
/*
Simple php udp socket client
*/
//Reduce errors
error_reporting(~E_WARNING);
// network settings for the wind vane
$server = '255.255.255.255';
$port = 55554;
if( isset($argv[1]) ) $server = $argv[1];
if( isset($argv[2]) ) $port = $argv[2];
if(!($sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "Socket created \n";
// Bind the source address
if( !socket_bind($sock, "127.0.0.1" , 55554) )
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n");
}
//Communication loop
while(1)
{
//Now receive reply from server and print it
if( ($bytesRead = socket_recvfrom ( $sock , $reply , 2045 , 0 , $server, $port )) === FALSE)
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
if( ! in_array($errorcode, array(35) ) )
die("Could not receive data: [$errorcode] $errormsg \n");
}
if( $bytesRead > 0 ) {
echo "Reply : $reply";
}
usleep(500000);
}