JustinK101 Posted October 6, 2009 Share Posted October 6, 2009 Hey All, I am looking for a code fragment to ping a UPD port, not TCP. So basically make sure there is something listening on the following end of a UPD port for a specific IP. What is the best way of doing this? //Something Like $status = ping_udp(8080, "192.168.1.4"); //Status either `true` or `false` Thanks. Link to comment https://forums.phpfreaks.com/topic/176658-code-to-ping-a-udp-port/ Share on other sites More sharing options...
Bricktop Posted October 6, 2009 Share Posted October 6, 2009 Hi JustinK101, You can't ping a UDP port, the best way to check a UDP port is to open a socket and check if you get a response. Have a look at this article for a tutorial on how to achieve this. Hope this helps. Link to comment https://forums.phpfreaks.com/topic/176658-code-to-ping-a-udp-port/#findComment-931341 Share on other sites More sharing options...
JustinK101 Posted October 6, 2009 Author Share Posted October 6, 2009 Bricktop, Here is the code I wrote, but it is always returning true, even though the port I am passing is not open. Ideas? <?php /** * Udp * Static Class * * @todo * * @version 1.0.0 * @author Justin * @copyright (c) 2009. All Rights Reserved. */ class Udp { /** * $p_host : String : IP address or fully qualified DNS hostnamae you wish to check * $p_port_number : Integer : UDP port you wish to check */ public static function isUp($p_host, $p_port_number) { $socket = fsockopen("udp://" . trim($p_host), trim($p_port_number)); if (!$socket) { return false; } return true; } } echo Udp::isUp("64.34.23.110", 3343); ?> Link to comment https://forums.phpfreaks.com/topic/176658-code-to-ping-a-udp-port/#findComment-931342 Share on other sites More sharing options...
JustinK101 Posted October 6, 2009 Author Share Posted October 6, 2009 OK, so I thought my problem was that I wasn't actually trying to write data to the socket, because UDP is stateless, but still my function is always returning 1 when I know it should return false. Any ideas guys? <?php /** * Udp * Static Class * * @todo * * @version 1.0.0 * @author Justin * @copyright (c) 2009. All Rights Reserved. */ class Udp { /** * $p_host : String : IP address or fully qualified DNS hostnamae you wish to check * $p_port_number : Integer : UDP port you wish to check * $timeout : Integer : OPTIONAL (10) : The number of seconds before the connection times out */ public static function isUp($p_host, $p_port_number, $p_timeout = 10) { $socket = fsockopen("udp://" . trim($p_host), trim($p_port_number), $error_number, $error_message, trim($p_timeout)); if (!$socket) { return false; } socket_set_timeout($socket, $p_timeout); $socket_write = fwrite($socket, "\x00"); if (!$socket_write) { return false; } return true; } } echo Udp::isUp("64.34.23.110", 3343); ?> Link to comment https://forums.phpfreaks.com/topic/176658-code-to-ping-a-udp-port/#findComment-931349 Share on other sites More sharing options...
JustinK101 Posted October 6, 2009 Author Share Posted October 6, 2009 Does it matter that the server I am trying to udp `ping` is running Windows 2003 Server? Link to comment https://forums.phpfreaks.com/topic/176658-code-to-ping-a-udp-port/#findComment-931350 Share on other sites More sharing options...
Bricktop Posted October 6, 2009 Share Posted October 6, 2009 Doesn't the "return true;" need to be in an else statement? Link to comment https://forums.phpfreaks.com/topic/176658-code-to-ping-a-udp-port/#findComment-931351 Share on other sites More sharing options...
JustinK101 Posted October 6, 2009 Author Share Posted October 6, 2009 Bricktop, No, because the other checks return false, in which case execution of the function stops. The only way to get to the end to return true is if none of the if checks hold valid. Link to comment https://forums.phpfreaks.com/topic/176658-code-to-ping-a-udp-port/#findComment-931353 Share on other sites More sharing options...
JustinK101 Posted October 7, 2009 Author Share Posted October 7, 2009 <?php class Udp { /** * $p_host : String : IP address or fully qualified DNS hostnamae you wish to check * $p_port_number : Integer : UDP port you wish to check * $timeout : Integer : OPTIONAL (10) : The number of seconds before the connection times out */ public static function isUp($p_host, $p_port_number, $p_timeout = 10) { $socket = fsockopen("udp://" . trim($p_host), trim($p_port_number), $error_number, $error_message, trim($p_timeout)); if (!$socket) { return false; } socket_set_timeout($socket, $p_timeout); $socket_write = fwrite($socket, "\x00"); if (!$socket_write) { return false; } return true; } } echo Udp::isUp("64.34.23.110", 3343); ?> You guys have any idea why the above code is always returning true, even though the UDP port I am testing is not open. Thanks. Link to comment https://forums.phpfreaks.com/topic/176658-code-to-ping-a-udp-port/#findComment-932083 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.