PHP套接字可以在Web服务器上关闭端口吗? [英] Can PHP socket work on closing port on web server?

查看:72
本文介绍了PHP套接字可以在Web服务器上关闭端口吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我假设:如果我的网络服务器上的端口2036关闭。我可以在PHP代码上使用函数 socket_bind($ sock,$ address,2036)吗?谁能解释一下这个问题呢?



我尝试过的事情:



我尝试了很多端口,但它仍然无效

I assume that: If port 2036 close on my web server. Can I use function socket_bind($sock, $address, 2036) on PHP code? Can anyone explain me about this question?

What I have tried:

I have tried a lot of port but it still doesn't work

推荐答案

sock,


地址,2036) 关于PHP代码?谁能解释一下这个问题呢?



我尝试过的事情:



我尝试了很多端口,但它仍然无效
address, 2036) on PHP code? Can anyone explain me about this question?

What I have tried:

I have tried a lot of port but it still doesn't work


目前还不是很清楚你的问题是什么。



如果关闭服务于特定端口的服务器并且想要重新启动该服务器,则必须注意一些事情。



关闭(关闭套接字)时可能仍有打开的连接。因此服务器应首先关闭活动连接。那些连接将处于TIME_WAIT状态。这意味着它们不是立即关闭,而是在一段时间后关闭:当收到FIN发送前的FIN表示关闭或超时后(默认为2 * MSL /最大Segemnt时间,默认为2分钟)。所以你必须等待这段时间才能将相同的端口绑定到新的套接字或者只是尝试它(在TIME_WAIT状态下仍然存在连接时绑定将失败)。



但是你可以通过使用SO_REUSEADDR套接字标志强制忽略TIME_WAIT状态的连接,该标志必须在绑定套接字之前设置(参见 PHP:socket_set_option - 手册 [ ^ ])。应始终为服务器套接字设置此标志。但即使使用它,您应该关闭所有连接并等待一段时间才能关闭套接字。这有望关闭与客户的大多数联系。
It is not quite clear what your problem is.

If you shutdown a server that is serving a specific port and you want to restart that server you have to observe some things.

There may be still open connections when shutting down (closing the socket). So the server should close active connections first. Those connections will be then in TIME_WAIT state. That means that they are not closed immediately but after some time: When receiving ACK for the FIN send before to indicate closing or after a time out (default is 2 * MSL / Maximum Segemnt Time which is 2 minutes by default). So you have to wait this time before binding the same port to a new socket or just try it (binding will fail while there are still connections in TIME_WAIT state).

But you can force ignoring of connections in TIME_WAIT state by using the SO_REUSEADDR socket flag which must be set before binding the socket (see PHP: socket_set_option - Manual[^] ). This flag should be always set for server sockets. But even when using it, you should close all connections and wait some time before closing the socket. This will hopefully close most connections to clients.


这篇关于PHP套接字可以在Web服务器上关闭端口吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆