Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socket 两处阻塞有点没明白,请大佬解释一下 #25

Open
dchaofei opened this issue Jul 18, 2018 · 6 comments
Open

socket 两处阻塞有点没明白,请大佬解释一下 #25

dchaofei opened this issue Jul 18, 2018 · 6 comments

Comments

@dchaofei
Copy link
Contributor

32

@elarity
Copy link
Owner

elarity commented Jul 18, 2018

首先是listen socket本身是阻塞的
其次是accpet是阻塞的

@dchaofei
Copy link
Contributor Author

恩, "listen socket 本身是阻塞" 这个我有点不明白,

// 创建一个监听socket,这个一个阻塞IO的socket
$listen = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
socket_bind( $listen, '0.0.0.0', 9999 );
socket_listen( $listen );

echo "test.",PHP_EOL;

while( true ){
    $connect = socket_accept( $listen );
}

如果 listen socket 是阻塞的,为什么会先把 test 打印出来

@elarity
Copy link
Owner

elarity commented Jul 18, 2018

不太容易解释,我这么说如果你把listen设置为非阻塞的,那么监听期间就只能应对一个客户的链接,如果恰巧此时有第二个客户,第二个就得等了。阻塞并不是说前三行代码阻塞,而是这个listen socket(本质上是一个文件)是阻塞的,是指这个意思。

@dchaofei
Copy link
Contributor Author

越来越懵了。。。
我理解的是当为 "阻塞时" ,监听期间如果有两个客户链接,第二就得等了。

@elarity
Copy link
Owner

elarity commented Jul 18, 2018

可能是我解释的不太好,文本表达有限。你可以去php.net查询 socket_set_nonblock函数,那上面有解释

@dchaofei
Copy link
Contributor Author

好的, 谢谢解释。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants