2009-09-17 144 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