How to disconnect from a telnet session using PHP ?Using this below code i read the telnet output.But i want to disconnect from telnet session if data can’t receive in $output variable within 3 second. How i do that.Please see my code below:
$fp = fsockopen("localhost", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
$output=fread($fp,128);//Here we read the output value from socket
fclose($fp);
echo $output;
}
Are you running under linux or Windows? In Linux you could fork the application to run the telnet session in parallel and have the main thread terminate the session after a given time if no output is received.
In Windows you could probably do a loop that does an
fread()on the socket and keep track of time spent without getting any data and closing the socket after a given time.Edit:
I just remembered that you don’t have to keep track of this manually 🙂
You could probably do something like