2011-05-26 88 views
1

如何從使用PHP的telnet會話斷開連接?使用下面的代碼我讀取了telnet輸出。但是如果數據無法在$輸出變量中接收,我想斷開與telnet會話的連接在3秒內。我如何做到這一點。請看我的代碼如下:如何使用PHP超時telnet會話

$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;    
} 

回答

2

你在Linux或Windows下運行?在Linux中,您可以分叉應用程序以並行運行telnet會話,如果沒有收到輸出,主線程會在給定時間後終止會話。

在Windows中,您可能會做一個循環,在套接字上執行fread(),並跟蹤所花費的時間,而無需獲取任何數據並在給定時間後關閉套接字。

編輯: 我只記得,你不必跟蹤此手動:) 你也許可以做到像使用Windows

// Set socket timeout to 10 seconds 
socket_set_timeout($fp, 10) 

... 

$output=fread($fp,128);//Here we read the output value from socket 
if($output == false) { 
    // Code to run if timeout occured 
    echo "We timed out!"; 
} 
... 
+0

thanks..im system.Couldü請指南我如何創建循環,並設置時間差異??請指導和幫助.. – riad 2011-05-26 07:22:34

+0

謝謝它的工作.... – riad 2011-05-26 08:13:19