2010-02-09 16 views

risposta

63

io non sono sicuro di cosa si intende per essere "trasmessa correttamente", ma si spera che questo esempio farà il trucco:

$host = 'stackoverflow.com'; 
$ports = array(21, 25, 80, 81, 110, 443, 3306); 

foreach ($ports as $port) 
{ 
    $connection = @fsockopen($host, $port); 

    if (is_resource($connection)) 
    { 
     echo '<h2>' . $host . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') is open.</h2>' . "\n"; 

     fclose($connection); 
    } 

    else 
    { 
     echo '<h2>' . $host . ':' . $port . ' is not responding.</h2>' . "\n"; 
    } 
} 

uscita:

stackoverflow.com:21 is not responding. 
stackoverflow.com:25 is not responding. 
stackoverflow.com:80 (http) is open. 
stackoverflow.com:81 is not responding. 
stackoverflow.com:110 is not responding. 
stackoverflow.com:443 is not responding. 
stackoverflow.com:3306 is not responding. 

Vedi http://www.iana.org/assignments/port-numbers per un elenco completo dei numeri di porta.

+0

Grazie - questo è molto simile a quello che mi aspettavo di fare. Quando ho detto "inoltrato correttamente" intendevo che l'utente ha abilitato il port forwarding sul proprio router per la porta che desidero utilizzare. Ho un gioco multiplayer che ha la possibilità di agire come un server. Quindi, è necessario il port forwarding e vorremmo avere una semplice funzionalità che provi a verificare che la porta sia, di fatto, inoltrata. –

+2

Ho ragione nel ritenere che questo controlli il tuo outbound e i ricevitori in entrata, quindi se volessi scrivere una funzione per testare il tuo outbound, devi trovare un server pubblico con tutte le porte in ingresso aperte? – Programster

+0

soluzione impressionante. (y) – Bhavin

Problemi correlati