-2

I am using Delphi I have a device. Device has ethernet card.I give a ip adress and port to device(Server ip adress). Device send data with tcp/ip every second to server.

I have to Listen device from Server.I have 1200 devices What is the best way listen 1200 device from one server? (Multi thread or another way)

looper
  • 1,929
  • 23
  • 42
user2180946
  • 67
  • 1
  • 3
  • 2
    I'd guess a simple loop could easily cope with 1,200 connections but it depends what you're doing with the data i.e. how much work the thread is doing after it's received each packet. It'll be simplest to implement with a single thread I'd think and then upgrade to a thread pool later if necessary. The alternative might be to use asynchronous sockets with callbacks if Delphi supports that (I don't know) and let it worry about the scheduling and which threads get involved, but it may be more flexible to manage it yourself. – Rup Apr 10 '13 at 11:32
  • 1
    I think the real questions are: what have you tried? **and** how much work does the server have to do for each connection? –  Apr 10 '13 at 11:37
  • 1200 threads is close to Windows limits. you should ind library that can listen 1200 connections from 1-4 threads, some library based on actor model or async events or i/o completion ports, like that. Perhaps it even does not have to be Delphi, just some intermediate server receiving data packets and sending them to your server via some IPC like named pipes – Arioch 'The Apr 10 '13 at 12:06
  • I think one or at max a few listening threads should be fine, then spawn a thread when a device is actually doing something and close it again when done. – Remko Apr 10 '13 at 14:32
  • Re-adding deleted links about threads: http://www.deltics.co.nz/blog/?p=1297 and http://www.deltics.co.nz/blog/?p=1330 – Arioch 'The Apr 10 '13 at 15:39

2 Answers2

3

Event-driven server is a requirement here.

Forget about Indy, and use an event-driven (aka over I/O completion) IP server.

Windows limit is around 2000 threads for 32 bit, since each thread reserves 2 MB of stack space, AFAIR. A server like Indy will use one thread per connection, so it will be just a big waste of resources.

By design, I/O completion allows to balance all the incoming requests to a small thread pool. See Is there a I/O completion port based component for Delphi? and Scalable Delphi TCP server implementation

Community
  • 1
  • 1
Arnaud Bouchez
  • 42,305
  • 3
  • 71
  • 159
  • I believe that Indy 10 does claim to be used in cases with >1K incoming connections, however +1 because I know you speak from experience. However I expect Remy will say indy 10 servers can be coded to accept >1.5K clients. – Warren P Apr 10 '13 at 14:09
  • 2
    @WarrenP Is I/O completion finished in Indy? Without it, it will rely on one thread per connection, and I'm afraid this feature is not implemented yet. Sounds pretty close to the well-known [DataSnap syndrome](http://robertocschneiders.wordpress.com/2012/11/22)... – Arnaud Bouchez Apr 10 '13 at 14:27
  • Yeah, I guess it's not. Thanks for the blog post link there I hadn't read that. Key quote: *Apparently, the structure of the HTTP communication framework makes use of the Indy components, which creates a thread for each HTTP request. This behavior causes the server to create and destroy dozens of threads per second, which causes a huge overhead. This overhead is certainly the cause of poor performance of the framework. Hard to say which is the cause of the problem that crashes the server but I believe it has a connection.* – Warren P Apr 10 '13 at 14:36
  • This related question has an answer suggesting Synapse has IOCP capability: http://stackoverflow.com/a/2290778/84704 – Warren P Apr 10 '13 at 14:40
  • 1
    Indy does not have any IOCP capabilities. That feature was attempted and scraped years ago and has not been revisited since. Under the current model, using 1500 simultaneous clients in an Indy server would put a strain on OS resources in 32bit. As for DataSnap, it does use Indy, but it doesn't utilize Indy's threadpool capabilities yet AFAIK. – Remy Lebeau Apr 10 '13 at 15:31
  • Okay so there is some thread-pooling capability in Indy but not IOCP. Thanks for the update Remy. – Warren P Apr 10 '13 at 22:51
2

Check out ICS which is an internet library which allows you to manage many connections from a single thread. Ideal for your purposes.

mj2008
  • 6,647
  • 2
  • 38
  • 56
  • iirc ICS uses the main Windows message loop for all connections, which might be a bottleneck – mjn Apr 10 '13 at 18:18
  • @mjn You can use it in a thread, so I presume that it is able to do messaging within that too. Certainly worth checking if it is a concern (I know it handled things fine for my situation within a thread). – mj2008 Apr 11 '13 at 11:48