Jump to content

Read a UDP Stream of Data


SDII_Davis

Recommended Posts

Hi! I am having trouble reading a stream of data being sent to my PC via PHP5. It seems that my code stops when it gets to the read section and just sits there indefinately. I am somewhat new to PHP so a solution and explanation would be great!

 

<?php
$host = "129.000.00.01";	// host IP address
$port = 40000;			// port to listen to

// sets script execution limit
set_time_limit(30);		// 0 means unlimited execution time (constant running)

// create UDP socket
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP) 
or die("Could not create UDP socket!\n");

// reset socket for binding
if(!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo "Failed to reset socket!\n";
} 
else echo "Reset socket successfully!\n";

// bind socket
socket_bind($socket, $host, $port);	

// connect
if (!socket_connect($socket, $host, $port)) {
echo "Failed to connect to port!\n";
}
else echo "Connected to port successfully!\n";

while (1) {
echo socket_read($socket, 1024, PHP_NORMAL_READ)."\n";
}

// close sockets
socket_close($socket);
echo "\nDone";

Link to comment
https://forums.phpfreaks.com/topic/235954-read-a-udp-stream-of-data/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.