Daily Archives: November 29th, 2006

(PHP code) Gracefully handling the failure of TCP resources

function check_tcp_active($host, $port) { $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array( “sec”=>0, “usec”=>500 ) ); $result = @socket_connect($socket, $host, $port); if ( $result ) { socket_close($socket); return(TRUE); } else { return(FALSE); } } function find_active_server($array) { // Format: $array['127.0.0.1']=3306 if ( is_array($array) ) { foreach ( $array as $host => $port ) { [...]