Last active
December 10, 2015 15:18
-
-
Save jjok/4453058 to your computer and use it in GitHub Desktop.
Test a socket connection.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Socket test. | |
* <code>php socket-test.php <server> <port></code> | |
* <code>php socket-test.php example.com 25</code> | |
* @author Jonathan Jefferies | |
*/ | |
try { | |
$hostname = isset($_SERVER['argv'][1])? $_SERVER['argv'][1]: 'localhost'; | |
$port = isset($_SERVER['argv'][2])? $_SERVER['argv'][2]: 80; | |
printf(' ? Attempting to connect to: %s:%u', $hostname, $port); | |
if(!is_resource(@fsockopen($hostname, $port, $code, $message, 5))) { | |
throw new RuntimeException($message, $code); | |
} | |
printf("\r:) Successfully connected to: %s:%u", $hostname, $port); | |
} | |
catch(Exception $e) { | |
printf("\nError: %u - %s", $e->getCode(), $e->getMessage()); | |
exit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment