java中的套接字不活动超时

发布于 2024-08-06 01:18:03 字数 296 浏览 3 评论 0原文

我在套接字 java API 中找不到关闭打开 TIMEOUT 毫秒的非活动连接的方法。 目标是避免当我忘记显式关闭套接字(并强制重新启动远程资源)时出现令人尴尬的错误。

如果我找不到低级方法,我将向我们的连接添加上次使用的值,并定期检查它......但我发现这很难看。

谢谢,

安东尼

P.S.我不想要连接超时,我不确定是否理解setSoTimeout,但我很确定这不是我们想要的。

PPS 当然,避免忘记关闭更好,但在一个大项目+大团队中......我们试图找到一种 100% 安全的方法。

I can't find in the socket java API a way to close a inactive connection open for TIMEOUT millisecond.
The goal is to avoid an embarassing error when I forget to close the socket explicitly (and force the distant resource to be rebooted).

If I don't find a low level method, I will add a last-time-used value to our connection, and check it periodically... but I find that ugly.

Thanks,

Antoine

P.S. I don't want the connection timeout, and I'm not sure to understand the setSoTimeout, but I'm quite sure it's not what we want.

P.P.S. Sure, avoid to forget the close is better, but in a big project + big teams...we try to find a 100% secure way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

粉红×色少女 2024-08-13 01:18:03

尝试打开 SO_KEEPALIVE:

s.setKeepAlive(true);

设置保持活动状态后,客户端应该偶尔(通常一到两个小时)将测试数据包发送回服务器。如果在几分钟内没有收到响应,它将假定服务器已关闭连接而没有通知它,并将自行关闭。

无论如何,这应该可以在您的服务器崩溃的情况下起作用。我不确定如果您的服务器仍在运行,它是否会起作用 - 只是忽略客户端。

如果只跟踪您创建的所有套接字并让一个线程定期检查每个套接字以查看它是否仍然打开并且最近是否已使用过,那就好多了。我不认为这很丑陋,我认为这可能是处理这个问题的最佳方法。

Try turning on SO_KEEPALIVE:

s.setKeepAlive(true);

With keep alive set the client should occasionally (commonly one or two hours) send a test packet back to your server. If it doesn't receive a response with in some number of minutes it will assume the server has closed the connection with out notifying it and will close itself.

That should work in case your server crashes at any rate. I'm not sure if it will work if your server's still running - just ignoring the client.

It would be far, far better just to keep track of all the sockets you create and have a thread periodically check each one to see if it's still open and has been used recently. I don't think that's ugly, I think it's probably the best way to handle this.

横笛休吹塞上声 2024-08-13 01:18:03

也许,您可以创建一个 Socket Wrapper 或一个观察者,以便在初始化 Socket 实例时使用。

使用 Aspcts (AOP),您可以将所有 Socket 创建调用作为切入点拦截,并启动一个新的 TimeoutObserver 作为 Aspect。

此 TimeoutObserver 包含您自己的规则的实现,以检查套接字是否打开或关闭。

当您对企业软件中已实现的和新的实现感到安全时,该解决方案的一个优点是非侵入性 AOP 模型。

也许,这就是处理这个问题的一种方式。

[]的,

maybe, you can create a Socket Wrapper that or an Observer for use when you initialize a Socket instance.

With Aspcts (AOP), you can intercept all your Socket creation calls as pointcuts, and start a new TimeoutObserver as an Aspect.

This TimeoutObserver contains implemention for your own rules to check if the socket are opened or closed.

An advantage f that solution, is the not intrusive AOP model, when you are safe about the already implemented and new implementations in your corporative software.

Maybe, thats a way to treat that question.

[]'s,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文