简体   繁体   English

如何使用 ASP.NET 和 C# 管理大量 TCP 连接

[英]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我的问题

  • How can I manage these connection?我该如何管理这些连接? Should I add those to a static list one by one when a TCP socket is successful?当 TCP 套接字成功时,我是否应该将它们一一添加到 static 列表中?
  • How can I know that a certain connection is dropped/disconnected?我怎么知道某个连接被丢弃/断开了?
  • How can I send certain requests on different ports?如何在不同的端口上发送某些请求? Let's say if a>b send it on port 4000 else if a<=b send it on 4001假设 a>b 在端口 4000 上发送,否则如果 a<=b 在 4001 上发送
  • How can I make it asynchronous?我怎样才能使它异步?

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假设

  • You are connecting to another client's server.您正在连接到另一个客户端的服务器。
  • You have a large volume of web traffic from either multiple machines or from multiple working processes on any given machine.您有来自多台机器或任何给定机器上的多个工作进程的大量 web 流量。
  • You know how to create TCP client server objects and handle the connections.您知道如何创建 TCP 客户端服务器对象并处理连接。

For less than 80 worker threads across your servers:对于跨服务器的少于 80 个工作线程:

  • Because each thread processes synchronously, you only need to use a single connection for each thread.因为每个线程都是同步处理的,所以你只需要为每个线程使用一个单独的连接。
  • If no single web server is running more than 20 worker processes, then you can designate a single port for each server to use.如果没有单个 web 服务器运行超过 20 个工作进程,那么您可以指定一个端口供每个服务器使用。 Stick the port in your web.config file as a variable and use that when creating connections.将端口粘贴在 web.config 文件中作为变量,并在创建连接时使用它。 You will never hit the limit.你永远不会达到极限。
  • Store your connection in a shared object that the entire app can use (could put this in your BLL layer) and if you have a connection error, re-create a new connection on that thread.将您的连接存储在整个应用程序可以使用的共享 object 中(可以将其放在您的 BLL 层中),如果您遇到连接错误,请在该线程上重新创建一个新连接。

For more than 80 worker threads across your servers:对于跨服务器的 80 多个工作线程:

  • Do the same as the last step but at this point you either need to negotiate for more connections or you will add a new layer in between your application and the server you wish to reach.执行与最后一步相同的操作,但此时您需要协商更多连接,或者您将在应用程序和您希望访问的服务器之间添加一个新层。
  • This second layer acts as a broker for the two sides and can manage a pool of connections instead and draws off a connection each time you need to access Server-A and puts it back into the pool when finished.第二层充当双方的代理,可以管理一个连接池,并在每次需要访问 Server-A 时提取一个连接,并在完成后将其放回池中。
  • Anytime you connect to the broker application, spawn a new thread to do the processing until the connection is dropped or closed.每当您连接到代理应用程序时,都会产生一个新线程来进行处理,直到连接断开或关闭。
  • Keep track of your open connections and viola you can have as many clients as you need but your bottleneck will be those 80 connections out even if you have hundreds or thousands in.跟踪您的开放连接和中提琴,您可以拥有任意数量的客户端,但您的瓶颈将是这 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# 中创建高性能服务的最佳方式,因为:

  • It does not block IO peripherals, as can be sockets.它不会阻塞 IO 外设,就像 sockets 一样。
  • Minimize the threads and improve the performance to it.最小化线程并提高性能。

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:根据我的经验,这些是我的建议:

  1. Create a main threat to handle the main program.创建一个主要的威胁来处理主程序。

  2. 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,它实现了一个异步过程来接受连接,使用方法BeginAcceptEndAccept这里是如何使用它的示例。 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 ,使用BeginReadEndRead方法接收数据, BegineWriteEndWrite方法发送数据。 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的连接。

  1. 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,也可以是DictionaryKey-Value-Pair集合,来存储每个 Client Class 并使用套接字 ID 作为访问每个 Client Class 的键。

  2. 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.由于每个客户端套接字都处理它自己的套接字,我用来实现一种在同一个客户端套接字上重新连接的方法,这样每个客户端都对自己负责。

  3. 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允许您使用BeginConnectEndConnect方法开始异步连接过程。

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.

相关问题 如何使用 c# 在 asp.net web 窗体中使用 WebSocket [TCP 连接] - How to work with WebSocket[TCP connection] in asp.net web form using c# ASP.NET 管理多个数据库连接 - ASP.NET manage multiple DB connections 需要有关在ASP.NET C#中使用的SQL连接的信息 - Need information on the connections for sql used in asp.net C# 如何在asp.net c#中计算网站的访问者数量 - how to count number of visitors for website in asp.net c# 根据Repeater ASP.NET C#中的条件管理超链接 - Manage Hyperlink base on the condition in Repeater ASP.NET C# 如何在ASP.NET Core中使用C#生成自定义键编号 - How to Generate Custom Key Number Using C# in ASP.NET Core 提供XML文件供asp.net c#中的大量记录下载 - Provide XML file for download with large number of records in asp.net c# 抽象大量类似的ASP.NET MVC操作(C#) - Abstracting a large number of similar ASP.NET MVC Actions (C#) 如何在 Asp.net mvc 中使用控制器的 Json 方法正确序列化 C# 长数? - How to correctly serialize C# long number using controller's Json method in Asp.net mvc? 如何为ASP.NET应用程序监视与远程计算机的连接数? - How to monitor number of connections to a remote computer for ASP.NET application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM