Can't receive data from UDP Socket

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
strboy
Forum Newbie
Posts: 1
Joined: Wed Aug 10, 2011 4:06 am

Can't receive data from UDP Socket

Post by strboy »

Hi all
I have to connect to delay circuit to command (On/Off/Polling) it. I need to use UDP Socket to communicate with deley circuit . I want to send command to delay circuit and receive the responsed message from delay circuit. My code can command circuit to On/Off but can't receive responsed message from it but when i use some software to check packet between my PC and Delay circuit I found "responsed message from deley circuit".

This is my code.

Code: Select all

<?php
	error_reporting(E_ALL | E_STRICT);

	$socket = socket_create(AF_INET, SOCK_DGRAM, 0);

	$packet="(0172040001EF)";  //On circuit #1
//   $packet = "(0172040000EG)"; //Off circuit #1
//   $packet="(0105C6)"; //Polling status of all circuit
	$len=strlen($packet);
	socket_connect($socket,"192.168.0.31",10001);
	socket_send($socket,$packet,$len,0);
	$buf = 'This is my buffer.';
	$from='';
	$port=0;
	if (false !== ($bytes = socket_recvfrom($socket, $buf, 248,0,$from,$port))) {
		echo "Read $bytes bytes from socket_recv(). Closing socket..."."<br/>";
	} else {
		echo "socket_recvfrom() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n";
	}
	echo $buf."<br/>";
?>
Post Reply