简体   繁体   中英

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.

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.

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
  • receive & send data through the connection
  • handle tcp timeouts/disconnects/etc & re-establish connection to the 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?

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/ .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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