2012-01-20 44 views
0

我目前在php中,使用非阻塞套接字連接到服務,我無法分辨連接因服務器端下來...所有我得到的錯誤是10035這是一個非致命的錯誤,我忽略了,但我從來沒有得到適當的錯誤,因此一直沒有能夠告訴當服務器關閉時連接已經下降...有關此事的任何建議?PHP套接字,告訴連接何時因服務器下線而死亡

我聽例行看起來像這樣

$this->shouldListen = true; 
$fullResponse = ''; 

while ($this->shouldListen) { 

    //read some data 
    $tempResponse=socket_read($this->sock,1000); 
    if ($this->shouldBeRunning() == false) { 
    break; 
    } 

    if ($tempResponse == FALSE) { 
    //ignore ignorable error 
    $error = socket_last_error($this->sock); 
    if ($error!=11 && $error!=115 && $error!=10035) { 
     $this->writeLogFile("got an error ".$error); 
     break; 
    } 
    } else { 
    //do something with the response 
    } 

} 

,創造像這樣

function connect() { 

    /* Turn on implicit output flushing so we see what we're getting 
    * as it comes in. */ 
    ob_implicit_flush(); 

    if (($this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) { 
    return false; 
    // echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; 
    } else { 
    //echo 'success1<br />'; 
    } 

    if (socket_bind($this->sock, '0.0.0.0') === false) { 
    return false; 
    //echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; 
    } else { 
    //echo 'success2<br />'; 
    } 

    if (socket_connect($this->sock, $this->ip, $this->port) === false) { 
    //echo "socket_connect() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; 
    return false; 
    } else { 
    //echo 'success3<br />'; 
    } 

    socket_set_nonblock($this->sock); 
    return true; 
} 

感謝套接字連接

丹尼爾

+2

你能展示一些代碼嗎? –

+0

@Pekka剛剛發佈了用於偵聽和創建套接字連接的代碼 – Daniel

+0

這是我更喜歡使用'fsockopen()'或'stream_socket _ *()'的原因之一,因爲您可以只調用'feof()在資源上檢測封閉的流。當然,如果服務器停機並且不發送「FIN」或「RST」,那麼您無法檢測到它,您只需實現超時/保持活動/確認系統。 – DaveRandom

回答

1

我們與一些遠程服務地方工作我們必須包含'ping'方法來測試我們所服務的服務重新連接仍然有一個活動的連接。我會建議在你的腳本中實現這個ping概念。

有人在PHP的socket_create頁面上發佈了使用socket_ *庫的實用程序ping方法。

+0

是的,我想如果沒有其他事情出現,就會這樣做 – Daniel

相關問題