I'm working on a web page, and I am hosting quite a few servers, some are TCP and some are UDP. I'd like to make them appear as online/offline on the page. I managed to make it work for TCP using <?php function ConnectTCP($PORT) { $serverConn = @stream_socket_client("tcp://127.0.0.1:$PORT", $errno, $errstr, 1); if ($errstr != '') { return false; } fclose($serverConn); return true; } ?>However this doesn't work with UDP, as it is connectionless.
I tried this, but it only returns a bunch of warnings and doesn't actually work<?php function ConnectUDP($PORT) { $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); socket_bind($socket, "127.0.0.1", $PORT); socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>1,"usec"=>0)); socket_recvfrom($socket, $buf, 12, MSG_OOB, $from, $PORT); if ($buf != '') { socket_close($socket); fclose($socket); return true; } socket_close($socket); fclose($socket); return false; } ?>Is there a way to accomplish the same as with TCP, either using PHP, or some other language that is intagratable with HTML?
Don't worry if plan A fails, there are 25 more letters in the alphabet ;)
What doing this function? function ConnectUDP($PORT) {
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($socket, "127.0.0.1", $PORT);
socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>1,"usec"=>0));
socket_recvfrom($socket, $buf, 12, MSG_OOB, $from, $PORT);
if ($buf != '') {
socket_close($socket);
fclose($socket);
return true;
}
socket_close($socket);
fclose($socket);
return false;
}
?>
It's my failed attempt at trying to detect if a given UDP port is open. You can disregard it completely. Don't worry if plan A fails, there are 25 more letters in the alphabet ;)
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |