actually i found this on the web, an thoguht i should give it a shot
<?php
// example of "daemon" in UDP
while(TRUE) { // never stop the daemon
$socketD = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); // create an UDP socket
echo 'Creating socket... '; // YES, the socket is creating EACH TIME the while loops
if($socketD === FALSE) { //omg, something went wrong
echo 'socket_create failed: '.socket_strerror(socket_last_error())."\n";
exit(1);
}
echo "OK\nBinding the socket on 192.168.1.2:3658 ... ";
// let the daemon listen on the port 3658 on all interfaces
if(!socket_bind($socketD, "192.168.1.2", 3658)) {
socket_close($socketD);
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
// this is a special way of socket_read()'ing what's on the socket once someone establishes a connection
socket_recvfrom($socketD, $buf, 65535, 0, $clientIP, $clientPort);
if($buf === FALSE) { // something went wrong
echo 'socket_read() returned false : '.socket_strerror(socket_last_error())."\n";
continue;
} elseif(strlen($buf) === 0) { // this should mean "client closed the connection"
echo 'socket_read() returned an empty string : '.socket_strerror(socket_last_error())."\n";
continue;
}
echo 'Incoming connection from '.$clientIP.':'.$clientPort.' ... ';
// ok now thats the tricky part
// if you want to reply to the client on the UDP socket it opened, you need to connect your listening socket on them
// this won't work unless the client puts their IP in the IP header of the packets it's sending to your socket
// UDP sessions don't require them to do so, and if they don't, you have no way to tell who's talking to you on the socket, and therefore no way to reply
// so if you want to interact with the client, you have to connect your socket on them, but by doing so, you're no longer listening to other incoming connections
// one way to fixe that would be to fork the script when an incoming connection arrives and then stop the forked script once the client exits
if(!socket_connect($socketD, $clientIP, $clientPort)) {
echo 'socket_create failed to connect the clientsock on '.$clientIP.':'.$clientPort.' : '.socket_strerror(socket_last_error())."\n";
socket_close($socketD);
continue;
}
echo 'connected'."\n"; // cool, now we can reply to the client
// now you can interact with the client using socket_write() and socket_read()
// once you're finished, you need to socket_close() your sockets and let the while loop and re-create the listenning socket (because you're no longer listenning for new incoming connections)
} // end of the infinite while
?>
however it says:
socket_bind failed: Only one usage of each socket address (protocol/network address/port) is normally permitted.
is it uses port 3658 to listen to port 3658?