在 C# 中处理此 TCP 连接的首选方法是什么?

发布于 2024-07-14 03:24:33 字数 696 浏览 7 评论 0原文

我有一个服务器应用程序(单一的、简单的 .NET 控制台应用程序),它与 GlobalCache GC-100-12 通信以路由 IR 命令。 本地网络上的各种 .NET WinForm 客户端连接到我的服务器应用程序并向其发送 ASCII 命令。 服务器应用程序将这些 ASCII 命令排队,然后通过 TCP 连接将它们发送到 GC-100-12。

我的问题是,从服务器的角度来看,处理此连接的最佳方法是什么? 我可以想到两种方法:

  1. 为每个单独的请求创建并打开一个新的 TcpClient。 请求完成后关闭 TcpClient。

  2. 在服务器启动时创建并打开一个 TcpClient,并使用保持活动状态(如果需要)在服务器对象的生命周期内保持连接打开。

我问这个问题是因为我想知道为每个请求创建一个新的 TcpClient 的开销。 这是一项昂贵的手术吗? 这是一个不好的做法吗?

目前我正在做#1,并将每次传输的结果打印到控制台。 有时,某些连接会超时并且命令不会被路由,我想知道这是因为每次创建新的 TcpConnection 的开销,还是由于其他原因。

我可以看到#2 更加复杂,因为如果连接确实断开,则必须重新创建它,并且这将需要更多代码来处理这种情况。

我正在寻找对此的任何一般建议。 我没有太多使用 TcpClient 类的经验。

I have a server application (singleton, simple .NET console application) that talks to a GlobalCache GC-100-12 for the purpose of routing IR commands. Various .NET WinForm clients on the local network connect to my server application and send ASCII commands to it. The server application queues these ASCII commands and then sends them to the GC-100-12 via a TCP connection.

My question is, what is the best way to handle this connection from the server's point of view? I can think of two ways:

  1. Create and Open a new TcpClient for each individual request. Close the TcpClient when the request is done.

  2. Create and Open one TcpClient when the server starts and use a keep-alive (if necessary) to keep the connection open for the lifetime of the server object.

I ask this question because I wonder about the overhead of creating a new TcpClient for each request. Is it an expensive operation? Is this a bad practice?

Currently I am doing #1, and printing the results of each transmission to the console. Occasionally some connections timeout and the command doesn't get routed, and I was wondering if that was because of the overhead of creating a new TcpConnection each time, or if it is due to something else.

I can see #2 being more complicated because if the connection does drop it has to be recreated, and that will require a bit more code to handle that circumstance.

I'm looking for any general advice on this. I don't have a lot of experience working with the TcpClient class.

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

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

发布评论

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

评论(1

瑾夏年华 2024-07-21 03:24:33

我们有一个类似的案例,打开一个到旧的基于 PICK 的系统的 telnet 会话。 我们发现每次收到请求时打开 TCP 连接的成本相当昂贵,因此我们决定实现一个无操作例程来保持连接打开。 它更复杂,但只要您的端点不尝试为许多客户端提供服务,那么固定连接听起来就是一个可行的解决方案。

如果您想防止在没有流量时保持连接打开,您还可以将其设置为超时。 五分钟没有任何活动,然后关闭连接。

We had a simillar case of opening a telnet session to an old PICK based system. We found that the cost of opening the TCP connection each time a request came in was fairly expensive, and we decided to implement a no-op routine to keep the connection open. It is more complex, but as long as your end point is not trying to serve many many clients then pinning a connection sounds like a viable solution.

You could also set it up to have a timeout, if you want to prevent keeping a connection open when there is no traffic. Five minutes of no activity then shut down the connection.

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