Jump to content

Code To Ping A UDP Port


JustinK101

Recommended Posts

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

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);
?>

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);
?>

<?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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.