简体   繁体   English

PHP:TCP连接监视

[英]PHP: TCP Connection Monitoring

I have an application that is used to connect ot a service once a week for a 1 hour session. 我有一个应用程序,用于每周一次连接一次服务,持续1小时。 This session has many requirements, one of which is that we only have one open tcp connection at a time. 该会话有很多要求,其中之一就是我们一次只能有一个打开的tcp连接。

Out of the 30 or so sessions we've completed we've had 3 sessions where there were multiple tcp connections open during the 1 hour. 在完成的30个左右会话中,有3个会话在1小时内打开了多个tcp连接。

I am wondering if there is a method in php which will return the connections state, I have considered using netstat and looking for the ip of the server we connect to and then determin by that status whether its okay to proceed or we should wait. 我想知道php中是否有一种方法可以返回连接状态,我已经考虑过使用netstat并寻找连接到的服务器的ip,然后根据该状态确定是否可以继续进行还是应该等待。 The problem with this is we have plans to purchase more connections in the future and that would result in only 1 connection being passed at a time. 问题是我们计划在将来购买更多的连接,并且一次只能传递1个连接。

We are using Zend_Http_Client from the zend framework and have tried the following: 我们正在使用来自zend框架的Zend_Http_Client,并尝试了以下方法:

unset the client and adapter object explicitly call the adapters close function after the clients request function the last time I did both unset and close the adapter 取消设置客户端和适配器对象后,上次我同时取消设置和关闭适配器后,在客户端请求函数后显式调用适配器的close函数

All the best, 祝一切顺利,

Jason 杰森

执行netstat命令并查看当前是否建立了到远程主机的任何连接,这可能是最简单的。

you shouldn't try to solve this in the client. 您不应该尝试在客户端中解决此问题。 limit concurrent connections in the server. 限制服务器中的并发连接。

Having revisited this problem I decided to use the bindto option in socket stream options to bind to the same port each time. 重新考虑了这个问题后,我决定每次在套接字流选项中使用bindto选项绑定到同一端口。 This should ensure that the previous connection was closed and the port freed. 这应确保先前的连接已关闭并且端口已释放。

Testing hasn't been completed but initial results do not show duplicate connections. 测试尚未完成,但初始结果未显示重复的连接。

http://ca.php.net/manual/en/context.socket.php http://ca.php.net/manual/zh/context.socket.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM