• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

socket_select(), 请大神讲授

php 搞代码 3年前 (2022-01-24) 13次浏览 已收录 0个评论

socket_select(), 请大神讲解
请大神帮忙解释一下 socket_select() 这个函数的作用,看php手册大致意思是阻塞的意思,请问什么情况下阻塞,什么时候继续往下执行啊,参数如下

<br />int socket_select ( array &$read , array &$write , array &$except , int $tv_sec [, int $tv_usec = 0 ] );<br /><br />//socket_select() accepts arrays of sockets a<em>8本文来源gao.dai.ma.com搞@代*码(网$</em><pre>搞代gaodaima码

nd 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

/*
read
The 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).

write
The sockets listed in the write array will be watched to see if a write will not block.

except
The sockets listed in the except array will be watched for exceptions.

tv_sec
The 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 的下一条语句,则必有一个套接字是需要你操作的


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:socket_select(), 请大神讲授

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址