Co chcem urobit, je, nacuvat na porte UDP/65136, pockat 5s na prichadzajuce data a tieto data zobrazit.
Kód: Vybrať všetko
$port = 65136;
$timeout=time();
while (time()<$timeout+5) // wait 5s for response
{
$socketD = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); // create an UDP socket
if($socketD === FALSE) { //omg, something went wrong
echo 'socket_create failed: '.socket_strerror(socket_last_error())."\n";
exit(1);
}
if (!socket_set_option($socketD, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo 'Unable to set option on socket: '. socket_strerror(socket_last_error())."\n";
}
if(!socket_bind($socketD, '0.0.0.0', $port)) {
socket_close($socketD);
echo 'socket_bind failed: '.socket_strerror(socket_last_error())."\n";
exit(1);
}
// so far, our socket is open and bound to a port: it's listening for ONE incoming connection
socket_set_nonblock($socketD);
// this is a special way of socket_read()'ing what's on the socket once someone establishes a connection
$len=socket_recvfrom($socketD, $buf, 2048, 0, $clientIP, $clientPort);
echo $len.' bytes received from '.$clientIP.':'.$clientPort.'<br>';
flush();
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;
} else echo $buf.'<br>';
flush();
socket_shutdown($socketD,2);
socket_close($socketD);
usleep(100000);
}
a na konciWarning: socket_recvfrom() [function.socket-recvfrom]: unable to recvfrom [11]: Resource temporarily unavailable in /srv/web/virtuals/tranceworld.cz/www/dcsearch/index.php on line 160
To, ze endpoint is not connected by som este pochopil (UDP je nespojovo orientovany), ale co to znamena ze Resource temporarily unavailable... nemam sajnu, preco je vlastne docasne nedostupny.Warning: socket_shutdown() [function.socket-shutdown]: unable to shutdown socket [107]: Transport endpoint is not connected in /srv/web/virtuals/tranceworld.cz/www/dcsearch/index.php on line 173
Btw. musi to byt UDP, lebo tak je specifikovany protokol, s ktorym sa snazim pracovat. S TCP som skusal urobit jednoduchy skript, ktory vypisoval to, co som napisal cez telnet a to fungovalo
Skusal som aj namiesto recvfrom pouzit socket_read(), ale to tiez nefungovalo. Ma niekto skusenosti?