永久 TCP 连接或请求处理时建立连接

发布于 2024-12-11 06:28:08 字数 252 浏览 0 评论 0原文

我正在开发一个 TCP 服务器,如果指定的任务完成,它将与客户端通信。所以我在服务器上打开一个套接字,客户端连接到它。
该连接也可用于将数据传输回客户端。这完全没问题。
但是连接中止和类似情况怎么办?

我的想法是每次客户端必须与服务器通信时都连接到服务器。但如何将数据发送回客户端?
我也应该在客户端打开一个套接字吗?

编辑:
我也考虑过WCF。我认为这可能是实现服务器客户端层次结构的一个非常好的方法。
你怎么认为?

I am developing a TCP server, which shall communicate with the client, if specified tasks are finished. So I open on the server a socket and the client connects on it.
That connection can be used for data tranfers back to the client, too. That is quite okay.
But what about connection aborts and anything like that?

My thought was to connect each time to the server, when the client have to communicate with it. But how can I send data back to the client?
Shall I open a socket on the client side, too?

EDIT:
I have considered WCF, too. I think it could be a very good way to implements server client hierarchy.
What do you think?

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

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

发布评论

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

评论(2

无风消散 2024-12-18 06:28:08

这取决于您的其余要求。如果我们谈论的消息不急,可能每天发送一次,那么正确的解决方案可能是客户端定期连接到服务器并检查是否有任何消息。如果我们谈论的是更常见、更匆忙的事情,正确的解决方案可能是客户端始终保持与服务器的连接打开。在某些情况下,正确的解决方案可能是服务器与客户端建立“向后”连接(如果可能)——也许可以选择在“向后”连接建立时回退到从客户端到服务器的持久连接不可能的。

请参阅这篇关于推送技术的文章,特别是关于长轮询

It depends on the rest of your requirements. If we're talking a message that is in no rush that might be sent once a day, the right solution might be for the client to connect to the server periodically and check if there are any messages. If we're talking something that's more common and more in a rush, the right solution might be for the client to keep a connection open to the server at all times. In some cases, the right solution might be for the server to make a 'backwards' connection to the client, if possible -- perhaps with an option to fall back to a persistent connection from the client to the server if the 'backwards' connection isn't possible.

See this article on Push technology, particularly the section on long polling.

墨小沫ゞ 2024-12-18 06:28:08

从运行时 POV 来看,服务器连接到客户端需要一个支持此操作的网络环境(防火墙/IDS 等)。

如果您不能确定情况总是如此,那么在 IMO 中就排除了此选项。

至于客户端保持连接打开:

我认为这是一个不错的选择...您需要确保客户端实现检测到任何连接问题并自动重新连接...

无论您实现什么解决方案,您可能都需要实现一个队列每个客户端的事件...根据您的要求,这些队列甚至可能需要持久...

WCF 可以以我描述的所有方式工作,并提供一些有助于构建的功能(例如序列化、可选会话管理、传输安全性等)一个强大且易于维护的系统......虽然是一个纯粹的基于 TCP/IP 的解决方案可能性能更好......

From a runtime POV having the server connect to the client needs a network environment supporting this (firewall/IDS etc.).

IF you can't be sure that this is always the case then this option is ruled out IMO.

As for the client keeping the connection open:

I think this is a good option... you need to make sure that the client implementation detects any connection problems and automatically reconnects...

Whatever solution you implement you might need to implement a queue of events per client... depending on your requeirements these queues might even need to be persistent...

WCF can work in all the ways I described and offers several things (like serialization, optional session management, transport security etc.) which help build a robust and well-maintainable system... although a pure TCP/IP-based solution might be better performance-wise...

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