請大神幫忙解釋一下 socket_select() 這個函數的作用,看php手冊大致意思是阻塞的意思,請問什麼情況下阻塞,什麼時候繼續往下執行啊,參數如下
int socket_select ( array &$read , array &$write , array &$except , int $tv_sec [, int $tv_usec = 0 ] );//socket_select() accepts arrays of sockets and waits for them to change status. Those coming with BSD sockets background //will recognize that those socket resource arrays are in fact the so-called file descriptor sets. Three independent arrays of //socket resources are watched.//param/*readThe sockets listed in the read array will be watched to see if characters become available for reading (more precisely, to see if a read will not block - in particular, a socket resource is also ready on end-of-file, in which case a socket_read() will return a zero length string).writeThe sockets listed in the write array will be watched to see if a write will not block.exceptThe sockets listed in the except array will be watched for exceptions.tv_secThe tv_sec and tv_usec together form the timeout parameter. The timeout is an upper bound on the amount of time elapsed before socket_select() return. tv_sec may be zero , causing socket_select() to return immediately. This is useful for polling. If tv_sec is NULL (no timeout), socket_select() can block indefinitely.*/
回複討論(解決方案)
你從哪裡看出是阻塞的?
socket_select 接受三個通訊端數組,分別檢查數組中的通訊端是否處於可以操作的狀態(返回時只保留可操作的通訊端)
使用最多的是 $read,因此以讀為例
在通訊端數組 $read 中最初應保有一個服務端監聽通訊端
每當該通訊端可讀時,就表示有一個使用者發起了串連。此時你需要對該串連建立一個通訊端,並加入到 $read 數組中
當然,並不只是服務端監聽的通訊端會變成可讀的,使用者通訊端也會變成可讀的,此時你就可以讀取使用者發來的資料了
socket_select 只在通訊端數組發生了變化時才返回。也就是說,一旦執行到 socket_select 的下一條語句,則必有一個通訊端是需要你操作的
謝謝大神,大致明白了