Page 1 of 1

fsockopen & connection timeout problem

Posted: Mon Aug 12, 2002 4:38 pm
by sKreeM
Hi there..

I've set a timeout of 1 second for the fsockopen function but the actual timeout is more like 15-20 seconds.

Is there something else I need to configure or do to have the timeout work correctly?

Thanks.

Posted: Mon Aug 12, 2002 6:23 pm
by Geschi
Are you sure you set a timeout of 1 second? Sometimes the server have lags.
Post your code, maybe we can help you then a bit better.

Posted: Wed Aug 14, 2002 12:31 pm
by sKreeM
Geschi wrote:Are you sure you set a timeout of 1 second? Sometimes the server have lags.
Post your code, maybe we can help you then a bit better.
<?
include("funcs.inc");
set_time_limit(0);

$http_check=1;
$ftp_check=0;
$port_scan=0;

$conn_timeout=1;

print date("H:i:s")."\n";

discoverhosts();

//#######################################
function DiscoverHosts() {
//#######################################
global $http_check, $ftp_check, $port_scan;

$scanfrom="1";
$scanto="3";
$ping_hosts=0;

$my_ip=gethostbyname("holmez");
$ips=explode(".",$my_ip);

for ($i=$scanfrom; $i<=$scanto; ++$i) {
$ip="$ips[0].$ips[1].$ips[2].$i";

if ($ping_hosts) {
exec("PING $ip",$ping);
if ($ping[3]!="Request timed out.") {
print "-------------------------\n";
print "-------------------------\n";

if (!$http_check) portscan($ip);
else CheckPort($ip);

}
} else if ($http_check) CheckPort($ip,"80");
else if ($ftp_check) CheckPort($ip,"21");
else if ($port_scan) portscan($ip);

}

//#######################################
function portscan($target) {
//#######################################
global $conn_timeout;
$f=fopen("ports.txt", "r") or die("Unable to open ports.txt for read access.");

while (!feof($f)) {
$port=trim(fgets($f,4096));
$connection=fsockopen($target,$port,$errnum,$errstr,$conn_timeout);
if ($connection) {
fclose($connection);
print "Port $port on $target\tOPEN\n";
}
print "Port $port on $target\tCLOSED\n";
}

fclose($f);
print "-------------------------\n";

}
}

//#######################################
function CheckPort($target,$port) {
//#######################################
global $conn_timeout;
$connection=fsockopen($target,$port,$errnum,$errstr,$conn_timeout);
if ($connection) {
print "Port $port on $target\tALIVE\n";
fclose($connection);
} else print "Port $port on $target\tDEAD\n";
}

print date("H:i:s")."\n";

?>