socket_select(), 請大神講解

來源:互聯網
上載者:User
請大神幫忙解釋一下 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 的下一條語句,則必有一個通訊端是需要你操作的

謝謝大神,大致明白了

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.