Hi guys. It's my first post. Sorry for my english...
An external device send some data on a port of my PC with the UDP protocol. I'm trying to create a "port scan" which does something when the data arrive at the port. I read on the manual that the socket_listen is not avalaible for UDP. They suggest to use socket_recv or socket_recvfrom, and I tried them, but I didn't get what I hope to get. Here's my code:
<?php
$host = '192.168.10.39';
$port = 2552;
$mySocket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); // create an UDP socket
echo 'Create socket... ';
if($mySocket === FALSE) {
echo 'socket_create failed: '.socket_strerror(socket_last_error())."\n";
exit(1);
}
echo "OK\nBinding socket on $host:$port ... ";
if(!socket_bind($mySocket, $host, $port)) {
socket_close($mySocket);
echo 'socket_bind failed: '.socket_strerror(socket_last_error())."\n";
exit(1);
}
echo "OK\nNow ready to accept connections.\nListening on the socket ... \n";
// so far, our socket is open and bound to a port: it's listening for ONE incoming connection
socket_set_nonblock($mySocket);
$timeout = time() + (10); // 10 seconds timeout
while (time() <= $timeout)
{
while (@socket_recv($mySocket, $data, 8192, 0))
{
echo $data;
}
usleep(100000); // 100ms delay keeps the doctor away
}
socket_close($mySocket);
?>
Port binding is ok (i can see the port with a netstat -an command during execution of script) but I can't get any data. I'm sure the communication is ok, since a very simple sniffer I wrote in C can get some data on the same port. Do you have some ideas?
Marco