Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building Multi threaded TCP/IP Server

I wanna build a TCP/IP server that will be used by up to 100 concurrent clients, but still not sure how to get started.

at least I need the server to this:

  1. Listening to client, and store all them on array or list.
  2. for each client, it need to receive and send data based on it's client status.
  3. The server should update the clients list when someone connect or disconnect.
  4. Prefer to work as service with GUI to manage it.

Could anyone help how to get start with that,I looked at indy sample but they didn't help, also looked for most components but still searching.

like image 600
DelphiDev Avatar asked Feb 18 '10 15:02

DelphiDev


People also ask

What is TCP multithreading?

Multithreading (Contd.) ∎ Multithreading allows a program to perform. multiple tasks concurrently. □ Although threads give the appearance of running. concurrently, in a single- processor system the interpreter is switching between the threads and running them one at a time.

Can TCP handle multiple clients?

The server can receive any number of connections on its single listening port, as long as a different address/port combination is used by each client.

What is multithreading server?

A multithreaded server is any server that has more than one thread. Because a transport requires its own thread, multithreaded servers also have multiple transports. The number of thread-transport pairs that a server contains defines the number of requests that the server can handle in parallel.

How a client and server environment can employ multithreading?

Multithreaded Server: A server having more than one thread is known as Multithreaded Server. When a client sends the request, a thread is generated through which a user can communicate with the server. We need to generate multiple threads to accept multiple requests from multiple clients at the same time.


2 Answers

You need to use the TidTCPServer which is multithreaded inside. No need for you to manage the threads. Everything is transparent, so in the way in which you write the application for one client, in (almost) the same way you will write it for many. See the OnConnect event. There is a TidContext parameter which has a TThreadList inside. You can use that event to 'register'/add your clients to your custom array/list and OnDisconnect to remove the clients.

The OnExecute event is triggered when the server receives a message. Use its parameters to read the message which is sent.

Also, you need another application which will be your client, using TidTCPClient. In this application you will set the address of your server (see the Host property) as well as the Port which should match with the server's one. You should call Connect (when the server is running) and to send strings you have SendCmd method. (Also see IOHandler.WriteLn if you want)

There are also other things but I think that's enough to get you started. Also you can post in Embarcadero's forums in the .Delphi.Winsock forum where the Indy team members are floating over. Or perhaps you can ask directly in .Delphi.Non-Technical and the guys there will guide you.

Another approach is DataSnap which is a more object-oriented layer over Indy (not to be confused with DBX) which gives your JSON, REST and other goodies. See for a small review here.

like image 123
John Thomas Avatar answered Jan 01 '23 22:01

John Thomas


And another Delphi library option would be synapse which provides a simple framework that can easily be extended. There is an IOCPPool demo available in the contributed files which may be of assistance.

Synapse is more of a framework of classes than a component library. There is a vibrant and active user community that is ready to support any challenges. I use this library in Delphi 2010 without any problems (although I use the latest development version from SVN).

Being as they are not components, it is very easy to use the classes in simple console applications or windows services.

like image 41
skamradt Avatar answered Jan 01 '23 22:01

skamradt