简体   繁体   English

通过TCP连接到多个客户端的服务

[英]A service that makes tcp connections to multiple clients

Most of the examples online are examples of a multithreaded tcp server, whereby the server listens for multiple incoming connections from the client. 在线上的大多数示例都是多线程tcp服务器的示例,通过该服务器,服务器侦听来自客户端的多个传入连接。

What I'm looking for is for the "server", to initiate the connections. 我正在寻找的是“服务器”,以启动连接。 If I have 100 devices (all different IP address of course), my server will have to make 100 individual connections to each device and receive data from the devices. 如果我有100个设备(当然,所有IP地址都不同),我的服务器将必须与每个设备建立100个单独的连接并从这些设备接收数据。

This is how I would make my connection to a device 这就是我与设备建立连接的方式

_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
_socket.SendTimeout = 2000;
_socket.ReceiveTimeout = 2000;
_socket.Connect(new IPEndPoint(IPAddress, TCPPort));

But I would like to know if there's any component out there, that would fulfill the following 但是我想知道是否有任何组件可以满足以下要求

  • establish as many tcp connections as I want to the devices 建立尽可能多的TCP连接到设备
  • receive & send data through the connection 通过连接接收和发送数据
  • handle tcp timeouts/disconnects/etc & re-establish connection to the IP 处理tcp超时/断开连接/等并重新建立与IP的连接

If there aren't any of such components, are there any tips as to how I should go about doing it? 如果没有任何此类组件,是否有关于如何进行此操作的提示? eg, 1 connection per thread, or how do I handle if the data is received in multiple packets, etc? 例如,每个线程1个连接,或者如果在多个数据包中接收到数据,该如何处理?

Any insights would be greatly appreciated 任何见解将不胜感激

There are several existing components out there that you could use to achieve what you want. 您可以使用几种现有组件来实现所需的功能。 Checkout http://www.networkcomms.net and http://code.google.com/p/lidgren-network-gen3/ . 检出http://www.networkcomms.nethttp://code.google.com/p/lidgren-network-gen3/

Having said that people always prefer to add that there is nothing stopping you writing everything from scratch yourself as per the example you give. 话虽如此,人们总是喜欢补充一点,没有什么可以阻止您按照所举的例子从头开始编写所有内容。

Disclaimer: I'm a developer for NetworkComms.Net. 免责声明:我是NetworkComms.Net的开发人员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM