PHP套接字服务器,检查客户端是否还活着 [英] PHP socket server, check if client is alive

查看:112
本文介绍了PHP套接字服务器,检查客户端是否还活着的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php服务器在监听1个c#客户端.

i have a php server listening for 1 c# client.

建立连接后,该连接将保持活动状态,直到客户端发送命令"quit"将杀死PHP服务器为止.

When a connection is established, it is kept alive until client send command "quit" wich kills the PHP server.

但是,当c#客户端在没有退出"命令的情况下断开连接(即:单击Windows窗体中的关闭(x)按钮)时,服务器将继续监听,并且无法从该客户端接收任何其他连接.

But when the c# client disconnect without the "quit" command (ie : clicking the close (x) button in the windows form) the server just keep listening, and cant receive any other connection from that client.

是否可以从服务器端(PHP)检查与客户端的连接是否仍然有效.

Is there a way to check from the server side (PHP) if connection is still alive with client.

我的php服务器代码基于以下示例: http://php.net/manual/zh-CN/sockets.examples.php

My php server code is based on exemple1 of : http://php.net/manual/en/sockets.examples.php

如果有人对重现错误/错误行为感兴趣,请从示例1中粘贴代码: http ://php.net/manual/zh-cn/sockets.examples.php ,从lan中的远程客户端通过telnet连接,拔掉客户端连线... php服务器将永远挂在身边,不接受任何新的连接

If someone is interested in reproducing the bug/error behavior, paste code from exemple1 : http://php.net/manual/en/sockets.examples.php, connect by telnet from a remote client in lan, unplug client wire... php server will hang around forever, no new connection is accepted

(对不起我的英语不好)

(sorry for my poor english)

推荐答案

在循环中,您需要检查

In your loop, you need to check the return value of socket_read(). If it returns FALSE, then there was a read error (which can be caused by the remote host closing the connection). The example code in the link you provided covers this case.

如果您需要适当地处理某些错误状态,则始终可以使用此说明描述可能的代码.

If you need to gracefully handle certain error states, you can always check the socket error code using socket_last_error() -- this note decribes the possible codes.

在将腻子用于telnet时,如果我用X按钮关闭,则在PHP中可以正确关闭连接,但是如果拔下腻子机的以太网线,PHP服务器就会挂起.

When using putty for telnet, if i close with X button, connecion is closed properly in PHP, but if i unplug the ethernet wire of the putty machine, PHP server just hangs around.

杀死PuTTY时关闭连接的原因是PuTTY退出时会关闭其打开的连接.这导致socket_read()返回错误代码(我相信ECONNRESET).如果您拉动网络电缆,则没有机会这样做.

The reason that the connection is closed when killing PuTTY is that PuTTY closes its open connection(s) when exiting. This causes socket_read() to return with an error code (I believe ECONNRESET). If you pull the network cable, it doesn't have a chance to do that.

取决于网络的配置方式,TCP连接应该最终会失败.您可以尝试通过设置 SO_RCVTIMEO 来控制超时 socket_set_option() ,但这并不总是适用于所有平台(我在看着你,WinSock).

Depending on how your network is configured, the TCP connection should eventually fail. You can attempt to control the timeout by setting SO_RCVTIMEO with socket_set_option(), but this doesn't always work on all platforms (I'm looking at you, WinSock).

或者,您也可以使用 socket_select() 来滚动自己的轮询循环合理的超时时间.如果超时后所有连接的套接字都没有要发送的数据,请终止服务器.

Alternatively, you can roll your own polling loop using socket_select() with a reasonable timeout. If none of the connected sockets have data to send after your timeout, then kill the server.

这篇关于PHP套接字服务器,检查客户端是否还活着的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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