[英]How to manage large number of TCP connections using ASP.NET and C#
I have an application which connects to a third party server let's call it Server-A.我有一个连接到第三方服务器的应用程序,我们称之为 Server-A。 I have been given four different ports ie 4000, 40001, 40002, 40003. On each port I can create 20 connections so I can create 80 total connections with server-A.
我获得了四个不同的端口,即 4000、40001、40002、40003。在每个端口上我可以创建 20 个连接,因此我可以创建与服务器 A 的总共 80 个连接。 I want to create a service layer that should communicate with server-A on mentioned ports.
我想创建一个服务层,它应该在提到的端口上与服务器 A 通信。 The technology will be asp.net C#.
该技术将是 asp.net C#。
The problem statement问题陈述
1- Application should be non-blocking/asynchronous to entertain 10 to 20 million request per day 1- 应用程序应该是非阻塞/异步的,以每天处理 10 到 2000 万个请求
2- Whenever the service layer starts it create 20 connections on each port. 2-每当服务层启动时,它会在每个端口上创建 20 个连接。 (Total 80 connections)
(共 80 个连接)
2- All connections should remain connected/alive 24/7 and reconnect whenever any connections drops/disconnects. 2-所有连接应保持连接/活动 24/7 并在任何连接断开/断开时重新连接。 It will send a heartbeat message in idle time.
它会在空闲时间发送心跳消息。
My Questions我的问题
For an initial start I created a single TCP connection on single port and it works as expected.首先,我在单个端口上创建了一个 TCP 连接,它按预期工作。 Then I replicated the same code for other port, but I know it is very bad approach and I have to copy same code 80 times to make 80 connections.
然后我为其他端口复制了相同的代码,但我知道这是非常糟糕的方法,我必须复制相同的代码 80 次才能建立 80 个连接。 I want a clean and scalable way to achieve it, so that in future may be I increase the connection to 100 or more.
我想要一种干净且可扩展的方式来实现它,这样将来我可能会将连接增加到 100 或更多。 Is there any framework which I can use?
有没有我可以使用的框架? Any help would be greatly appraised.
任何帮助都会受到极大的评价。
To handle such a large volume of traffic you need to do a few things.要处理如此大量的流量,您需要做一些事情。
Assumptions假设
For less than 80 worker threads across your servers:对于跨服务器的少于 80 个工作线程:
For more than 80 worker threads across your servers:对于跨服务器的 80 多个工作线程:
@Kartoos Khan, i have made some services with those requirements and using asynchronous methods is the best way to create high performance services in C#, because: @Kartoos Khan,我已经根据这些要求提供了一些服务,使用异步方法是在 C# 中创建高性能服务的最佳方式,因为:
Let me recommend you the book Writing High-Performance .NET Code .让我向您推荐《编写高性能 .NET 代码》一书。 The chapter 4, Asynchronous Programming has the information that you need to know to improve the performance.
第4 章,异步编程提供了提高性能所需了解的信息。
In my experience those are my recommendations:根据我的经验,这些是我的建议:
Create a main threat to handle the main program.创建一个主要的威胁来处理主程序。
Create a class to handle the Socket Server, which implements an asynchronous process to accept connections, using the methods BeginAccept
and EndAccept
, here is a sample of how to use it.创建一个 class 来处理 Socket Server,它实现了一个异步过程来接受连接,使用方法
BeginAccept
和EndAccept
, 这里是如何使用它的示例。 Create another class to handled the socket connections, which has as a property the Socket
object.创建另一个 class 来处理套接字连接,它具有
Socket
object 作为属性。
2.1 create a method to start the Reading process, which will be called by the Server class to start the communication between the endpoints. 2.1 创建一个方法来启动Reading进程,Server class会调用这个方法来启动端点之间的通信。 This methos will start the process of read in an asynchronous way.
此方法将以异步方式启动读取过程。
2.2 To read and write in an asyncrhonous way, it is necessary to get the NetworkStream
from the socket, and use the methods BeginRead
and EndRead
, to receive data, and BegineWrite
and EndWrite
to send data. 2.2 异步读写,需要从socket中获取
NetworkStream
,使用BeginRead
和EndRead
方法接收数据, BegineWrite
和EndWrite
方法发送数据。 Here there is the documentation. 这里有文档。
In the case that your service only needs to connect to a Server, ignore the step 1and implement the Client class to start the connection to an specific EndPoint.
如果你的服务只需要连接到一个Server,忽略第1步,执行Client class来启动到特定EndPoint的连接。
Use a collection class, as can be a Dictionary
, Key-Value-Pair collection, to store each Client Class and use the socket ID as the key to access to each Client Class.使用集合 class,也可以是
Dictionary
, Key-Value-Pair集合,来存储每个 Client Class 并使用套接字 ID 作为访问每个 Client Class 的键。
Due each Client Socket handles it own socket, i use to implements a way to reconnect at the same Client Socket, i this way each Client is responsable for itself.由于每个客户端套接字都处理它自己的套接字,我用来实现一种在同一个客户端套接字上重新连接的方法,这样每个客户端都对自己负责。
The main program will be responsable to create each Client Server and set the EndPoint of each client, as you need, and start to connect each of them.主程序将负责创建每个客户端服务器并根据需要设置每个客户端的端点,并开始连接每个客户端。 In this case,
TCPClient
allow you begin an asynchronous process for connect, using the methods BeginConnect
and EndConnect
.在这种情况下,
TCPClient
允许您使用BeginConnect
和EndConnect
方法开始异步连接过程。
Here you can see more details about this issue. 在这里,您可以查看有关此问题的更多详细信息。
I hope this might be useful for you.我希望这可能对你有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.