Skip to content

Instantly share code, notes, and snippets.

@jjok
Last active December 10, 2015 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjok/4453058 to your computer and use it in GitHub Desktop.
Save jjok/4453058 to your computer and use it in GitHub Desktop.
Test a socket connection.
<?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