2009-09-17 130 views

回答

4

socket_read()

 
socket_read() returns the data as a string on success, or FALSE on error 
(including if the remote host has closed the connection). The error code can 
be retrieved with socket_last_error(). This code may be passed to 
socket_strerror() to get a textual representation of the error. 

這是相當標準的做法,如果一個套接字在大多數語言封閉檢測。我不相信PHP提供了一個直接的事件風格的通知(PEAR中可能除外)。

+0

這將在非阻塞模式下完成! – transilvlad 2012-12-19 11:13:07

+0

@tntu:這是不正確的。如果沒有數據(包括非阻塞模式),則返回''「''。這與「錯誤」是分開的和不同的。 – 2012-12-19 14:15:14

+0

那麼在阻塞模式下,它不會阻塞,直到它收到的東西,而不是隻是檢查連接是否關閉? – transilvlad 2012-12-19 16:35:57

0

fread($socket)返回空字符串,瞬間,無需等待套接字超時。

(不,這不是一個真正的答案,但是這是hackish的解決方案,我在我的代碼ATM已經制定這種行爲也可能是針對Windows的。)

+1

這是大多數語言/平臺,不只是PHP/Windows標準的行爲。 – 2009-09-17 01:51:57

+0

如果在連接打開時調用'fread($ socket)',返回什麼? – 2009-09-17 02:10:24

+0

無論等待讀取什麼數據 – 2009-09-17 02:59:31