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

Link to comment
Share on other sites

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
Share on other sites

<?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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.