I am trying to make a code the same as the one which micro torrent use to check whether a port is open:
http://www.utorrent.com/testport.php?port=222

Just now I have this:

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$ip = "udp://$ip";
$fp = fsockopen($ip, 222, $errno, $errstr, 30);
if (!$fp) {
echo "$ip<br>Not Open";
} else {
echo "$ip<br>Open";
}
?> 

But it is not working as it always says open even if it is not. Where have I gone wrong. Is there another way to do this?

    If you read the PHP manual on [man]fsockopen/man, you will see the warning:

    UDP sockets will sometimes appear to have opened without an error, even if the remote host is unreachable. The error will only become apparent when you read or write data to/from the socket. The reason for this is because UDP is a "connectionless" protocol, which means that the operating system does not try to establish a link for the socket until it actually needs to send or receive data.

    So, you should try and read/write to/from the socket cautiously to determine if the connection really was made.

      I don't know how that would work because either way peer gurdian would block it on my computer so it would not be able to write. What would I be able to read and how would I do it?

      Would there be an alternative way to do this like with curl?

        You could try:

        <?php
        $hostname = 'udp://' . $_SERVER['REMOTE_ADDR'];
        echo $hostname . ' ';
        if ($fp = @fsockopen($hostname, 222, $errno, $errstr, 30)) {
            stream_set_blocking($fp, 0);
            echo (($str = fgets($fp)) !== false) ? 'open' : 'not readable';
            fclose($fp);
        } else {
            echo 'not open: ' . $errstr;
        }
        ?>

          Thanks for the help laserlight it is nice to see some females around :-p Unfortunately it did not work like before but I have found a script which does on this site:

              <?php
              //begin "scanner.php"
          
          //Now get the form input:
          $host = $_REQUEST['host']; 
          $start = $_REQUEST['start'];
          $end = $_REQUEST['end'];
          
          //Here comes the FOR loop.
          //We will be using a variable called
          //'$current' to keep track of our ports
          for($current = $start; $current <= $end; $current++)
          	{
          	//1.Get the name of the service associated
          	//with the current port for a TCP
          	//connection. You can also use UDP
          	//if you want by just changing the value
          	$service = getservbyport($current, "tcp");
          
          	//2.Now try and connect to the current port.
          	//If we are successful, $result will
          	//return true. Note that the '@' symbol in front
          	//of a PHP function keeps it from complaining
          	//and spitting out debugging info when the
          	//function fails.
          	$result = @fsockopen($host, $current);
          
          	//3.Now display the results of the connection attempt
          	echo<<<OUTPUT
          	Port: <b>$current</b>
          	 is commonly used for: <b>$service</b>
          	 and was: 
          
          OUTPUT;
              //make sure the word 'OUTPUT;' above is the only word
              //on that line, and has no leading or trailing spaces.
              //other wise it won't work!
          
          	if($result)
          		{
          		echo "<font color=\"green\"><b>OPEN</b></font><br>";
          		}
          		else
          			{
          			echo "<font color=\"red\"><b>CLOSED</b></font><br>";
          			}
          
          
          
          	}
          //end "scanner.php"
          ?>
          

          Don't scan too many ports though the script takes ages to run if the port is not open.

            You are now connecting via TCP, not using UDP. Is this what you intended in the first place?

              You cannot reliably determine whether a UDP port is open on any operating system with any software.

              An open UDP port does not need to respond to a message to it - nor does a closed one.

              Therefore, in the general case they are indistinguishable.

              However, if you are trying to test for a server for a specific protocol on that port, you can send some kind of handshake to that port (dependent on the protocol) and wait for a response - as it's UDP you'll have to handle retry and timeout yourself though.

              Mark

                laserlight wrote:

                You are now connecting via TCP, not using UDP. Is this what you intended in the first place?

                Well the original idea was to scan both types of ports but I though I would start with the one that would be most complicated, and just now I am thinking fairly impossible.

                But I have TCP done now so Scanning ports and finding out only those is good enough I suppose.

                  Write a Reply...