Building Multi threaded TCP/IP Server

前端 未结 7 2005
我在风中等你
我在风中等你 2021-02-08 03:42

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:

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-08 04:37

    Indy is your best choice : 1000 clients are not that much : I did develop a server that had to serve 4-5 k clients and it's working as a charm.

    1. Listening to client, and store all them on array or list. + 3. The server should update the clients list when someone connect or disconnect.

    --> As for the clients list, you could loop through the TThreadList member of TidTCPServer (version 9.0) that stores all the "alive" threads, each thread is "equivalent" to a client connection, although threads could outlive a client's connection, but you could fix this by setting an appropriate connection's timeout value. If you want you could also maintain your own Clients' List (inherit from TList for instance or create a Generics.Collection): you would add the client info's after the onConnect event (tidPeerThread class exposes all the client infos: IP...)

    You would then loop through this list periodically and check for alive connections (ping command like) and kill/delete all zombies.

    [indy documentation] Event handler for peer thread connection attempts.

    property OnConnect: TIdServerThreadEvent;

    Description

    OnConnect is an event handler for TIdServerThreadEvents. OnConnect occurs when a TIdPeerThread attempts to connect to a TIdTCPServer.

    OnConnect receives AThread as a parameter, representing the TIdPeerThread thread that is requesting the connection.

    Assign a TIdServerThreadEvent event handler procedure to OnConnect. [/indy documentation]

    1. for each client, it need to receive and send data based on it's client status : --> check the chat client and server demo source code for a detailed example.

    2. Prefer to work as service with GUI to manage it : you could develop a service application that would log all of its activity in a DB and a second app that would access that db and show all available stats (clients number...).

    and here are the links to Indy 9.0 (sources and documentation) : http://www.indyproject.org/downloads/Indy_9_00_14_src.zip http://www.indyproject.org/downloads/Indy-9-0-Help-WinHelp.zip

    And here is an Indy book although I don't think you would need it after reading the documentation: Indy in depth : http://www.atozed.com/Indy/Book/index.EN.aspx

    Look here for a good tutorial : http://www.devarticles.com/c/a/Delphi-Kylix/Creating-Chat-Application-with-Borland-DelphiIndy-The-Client/

    Good Luck

提交回复
热议问题