大容量TCP客户端的设计

发布于 2024-07-05 19:06:03 字数 335 浏览 6 评论 0原文

我有一个 .NET TCP 客户端,它向(.NET 异步)TCP 服务器发送大量消息。

我需要继续向服务器发送消息,但由于 TIME_WAIT,我用完了客户端上的端口。

程序如何在不使用所有可用端口的情况下持续可靠地发送消息?

有没有一种方法可以继续重复使用同一个套接字。 我查看了 Disconnect() 和 REUSEADDRESS 套接字标志,但找不到任何好的使用示例。 事实上,大多数消息来源都说不要使用 Disconnect,因为它用于较低级别的使用(即它仅回收套接字句柄)。

我在想我需要切换到 UDP 或者也许有一种使用 C++ 和 IOCP 的方法?

I have a .NET TCP Client that sends high volumes of messages to a (.NET async) TCP server.

I need to keep sending messages to the server but I run out of ports on the client due to TIME_WAIT.

How can a program continually and reliably send messages without using all of the available ports?

Is there a method to keep reusing the same socket. I have looked at Disconnect() and the REUSEADDRESS socket flag but cannot find any good examples of their use. In fact most sources say not to use Disconnect as it is for lower level use (i.e. it only recycles the socket handle).

I'm thinking that I need to switch to UDP or perhaps there is a method using C++ and IOCP?

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

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

发布评论

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

评论(6

乖不如嘢 2024-07-12 19:06:03

TCP 非常努力地防止网络拥塞。 所有新的 TCP 连接都以“慢启动”状态开始,在这种状态下,它们仅发送一个数据包并等待另一端的确认。 如果收到 ACK,TCP 将发送两个数据包,然后发送四个数据包,依此类推,直到达到最大窗口大小。

如果您以高数据速率生成消息,您确实希望避免打开和关闭 TCP 连接。 每次打开新连接时,您都会回到慢启动状态。 如果可以保持套接字打开,TCP 连接将克服慢启动状态,并能够以更高的速率发送数据。

为此,您需要让服务器在一个连接上处理多个消息(这意味着找到一种方法来描述每条消息)。 如果您的服务器支持任何类型的 HTTP 编码,那么这将起作用; 确保检查与“持久”连接或 HTTP 1.1 相关的任何参数或配置,因为这就是 HTTP 通过单个 TCP 连接发送多个请求的方式。

您提到的一种选择是 UDP。 如果您以相当高的速率生成消息,则可能会由于队列在途中某处已满而丢失其中一些消息。 如果您发送的消息需要可靠,UDP 可能不是一个好的基础。

TCP tries very hard to prevent congestion in the network. All new TCP connections begin in a "slow start" state, where they send only one packet and wait for an acknowledgement from the other end. If the ACK is received TCP will send two packets, then four, etc until it reaches its maximum window size.

If you are generating messages at high datarate, you really want to avoid opening and closing TCP connections. Every time you open a new connection you'll be back in slow start. If you can keep the socket open the TCP connection will get past the slow start state and be able to send data at a much higher rate.

To do this, you need to get the server to process more than one message on a connection (which means finding a way to delineate each message). If your server supports HTTP encoding of any sort this would work; make sure to examine any argument or configuration related to "persistent" connections or HTTP 1.1, because that is how HTTP sends multiple requests over a single TCP connection.

One option you mentioned is UDP. If you are generating messages at a reasonably high rate you're likely to lose some of them due to queues being full somewhere along the way. If the messages you are sending need to be reliable, UDP is probably not a good basis.

榆西 2024-07-12 19:06:03

当这样编码时,它不起作用。

服务器仅接收第一条消息。

当我打开和关闭套接字时,服务器可以工作,但我用完了客户端端口。

我猜是我的服务器的设计导致我必须像这样编写客户端代码。

那么如何使用 .NET 编写异步服务器代码呢? 我遵循了 MSDN 示例和大量在线示例。

When coded like that it doesn't work.

The server only receives the first message.

When I open and close the socket then the server works but I run out of client ports.

I'm guessing it's the design of my server that is causing me to have to code the client like that.

So how does one code an Async server using .NET. I have followed the MSDN examples and numerous examples online.

欢烬 2024-07-12 19:06:03

您的客户端可以保持同一个套接字打开并循环发送消息吗?

open socket connection

while(running)
    send messages over socket

close socket connection

Can your client just keep the same socket open and send messages in a loop?

open socket connection

while(running)
    send messages over socket

close socket connection
小帐篷 2024-07-12 19:06:03

如果服务器和客户端都知道数据的格式,则可以保持套接字打开。 您正在关闭套接字,以便服务器可以“看到”客户端已“完成”。

如果您有某种协议,那么服务器可以“知道”何时完成接收数据块。

您可以查找某种消息结束标记,您可以传入消息的长度,并根据大小等读取其余部分。不同的方法。

但是没有理由不断地打开和关闭与服务器的连接——这才是让你丧命的原因。

You can keep the socket open if your server and client are aware of the format of the data. You're closing the socket so that the server can "see" that the client is "done".

If you have some protocol, then the server can "know" when it's finished receiving a block of data.

You can look for an End-of-message token of somekind, you can pass in the length of the message, and read the rest based on size, etc. Different ways of doing it.

But there's no reason to constantly open and close connections to the server -- that's what's killing you here.

尤怨 2024-07-12 19:06:03

编写 TCP 服务器(使用任何语言)的基本思想是打开一个端口来侦听连接,然后创建新的线程或进程来处理新的连接请求。

open a server socket // this uses the port the clients know about

while(running)
    client_socket = server_socket.listen
    fork(new handler_object(client_socket))

这是一个很好的例子C#

The basic idea behind writing TCP servers (in any language) is to have one port open to listen for connections, then create new threads or processes to handle new connection requests.

open a server socket // this uses the port the clients know about

while(running)
    client_socket = server_socket.listen
    fork(new handler_object(client_socket))

Here's a good example in C#.

ペ泪落弦音 2024-07-12 19:06:03

消息队列服务之类的东西会对您的项目有利吗? 这样,您的客户端可以将尽可能多的消息传递到您的服务器,而您的服务器可以在可能的情况下以尽可能快的速度从队列中提取这些消息,如果您的客户端发送的消息超出了它可以处理的数量,它们就会变得简单进入队列等待处理。

快速谷歌搜索发现了这个关于使用 C# 构建消息队列服务的 MSDN 文档。

Would something along the lines of a message queuing service benefit your project? That way your client could pass as many messages to your server and your server could simply pull those messages from the queue when it can and as fast as your can and if you're client is sending more than it can handle, they'll simple enter the queue and wait to be processed.

Some quick Googling turned up this MSDN documentation on building a message queuing service with C#.

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