On 16 Mar 13:00, Mark Rogers wrote:
On 16/03/12 12:22, Mark Rogers wrote:
I have a device I need to talk to via UDP from within PHP (although it feels like my problem here is a Linux one not a PHP one as everything is being done at the socket level anyway).
I just tried this on PHP on Windows (albeit under VirtualBox on Linux) and it fails the same way. So it's not a Linux issue.
Anyone care to help me "convert" the PHP code to another language that I can test under Linux? It would be nice to see whether it's a PHP issue. (Can I do something similar from within bash?)
I have a suspicion that the Winsock DLLs under Windows are doing something that I'm not doing, but I have no idea what that might be.
The PHP code I'm struggling with is: // Create a socket $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); // Build the message $msg = hex2str('80 00 02 00 01 00 00 11 00 00 05 01'); // Send the message socket_sendto($socket, $msg, strlen($msg), 0, '192.168.251.132', '9600'); // Get the response - this line never returns socket_recvfrom($socket, &$buf, /*Max response length*/250, /*Flags*/0, &$ip, &$port);
Are you *sure* that there is going to be a return packet, and that your send packet *definitely* arrived at the other end?
The equiv in python is something like:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.settimeout(4)
msg_string = '80 00 02 00 01 00 00 11 00 00 05 01' msg = "".join([chr(int(b,16)) for b in msg_string.split(' ')]
s.connect(('192.168.251.132', 9600)) s.send(chars) data,addr = s.recvfrom(250)
Hope that helps,