Page MenuHomePhabricator
Paste P1887

Fast_reconnect.php
ActivePublic

Authored by Joe on Aug 17 2015, 9:14 AM.
Referenced Files
F1563078: Fast_reconnect.php
Aug 17 2015, 9:14 AM
Subscribers
None
<?php
function timedelta($mend, $mstart) {
list($uend, $send) = explode(" ", $mend);
list($ustart, $sstart) = explode(" ", $mstart);
$sdiff = (float)$send - (float)$sstart;
$udiff = (float)$uend - (float)$ustart;
return ($sdiff + $udiff);
}
/*
Let's test what happens to fsockopen if we don't get an answer after sending a SYN
*/
$start = microtime();
$timeout = 0.5;
$errno=$errstr= null;
$ips = array('192.168.1.104', '192.168.1.4');
foreach ($ips as $ip) {
$s = fsockopen($ip, 22, $errno, $errstr, $timeout);
if ($s) {
$end = microtime();
$tfirst = timedelta($end, $start);
echo "First socket opened within $tfirst\n";
fclose($s);
break;
}
}
$timeout = 0.03; // 30 milliseconds
$start = microtime();
foreach ($ips as $ip) {
$retries = 2;
for ($i=0; $i<$retries; $i++) {
$s = fsockopen($ip, 22, $errno, $errstr, $timeout);
if ($s) {
$end = microtime();
$tfirst = timedelta($end, $start);
echo "Second socket opened within $tfirst\n";
fclose($s);
break;
}
}
}
?>
joe@test:~$ php test.php
PHP Warning: fsockopen(): unable to connect to 192.168.1.104:22 (Connection timed out) in /home/joe/test.php on line 17
First socket opened within 0.508883
PHP Warning: fsockopen(): unable to connect to 192.168.1.104:22 (Connection timed out) in /home/joe/test.php on line 32
PHP Warning: fsockopen(): unable to connect to 192.168.1.104:22 (Connection timed out) in /home/joe/test.php on line 32
Second socket opened within 0.065743

Event Timeline

Joe changed the title of this paste from untitled to Fast_reconnect.php.
Joe updated the paste's language from autodetect to autodetect.
Joe added a project: acl*sre-team.