LNet - Creating a new thread per connected client. lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Logic_Bomb · 2011-08-24 20:20 Hey all, I have spent a while creating a new text based Game in Lazarus, Problem is, since the game is heavily centered around Player to Player interaction, the server constantly has to deal with user-data (Stored as INI), this means that upon receiving a command/query from the client it may have to open up & read this data meaning any new incoming queries/commands are forced to wait. What I would like to do is create a new thread instance every time a client is connected and this thread would then listen to that client & respond appropriately based on query. Does anyone have any idea on how to implement this, I have been using Lazarus (ObjPascal) for a while, but have only just recently ventured into the realms of Sockets & Networking. Any help would be greatly appreciated and if you need any more information please do not hesitate to ask. Thanks in advance. Logic~ ---------------------------------------------------------------------- eny · 2011-08-24 20:43 Well, you have already given the answer yourself: Quote from: Logic_Bomb on August 24, 2011, 10:20:01 pmWhat I would like to do is create a new thread instance every time a client is connected and this thread would then listen to that client & respond appropriately based on query. Start by reading the Wiki section about threads and go from there: http://wiki.lazarus.freepascal.org/Multithreaded_Application_Tutorial. QuoteAny help would be greatly appreciated and if you need any more information please do not hesitate to ask.If you have any more questions, just ask ---------------------------------------------------------------------- Logic_Bomb · 2011-08-24 20:50 Quote from: eny on August 24, 2011, 10:43:11 pmWell, you have already given the answer yourself: Start by reading the Wiki section about threads and go from there: http://wiki.lazarus.freepascal.org/Multithreaded_Application_Tutorial. Already done so, I've created threads in the past, I understand how to implement them in a static sense, (i.e. having a pre-defined set of threads that can be run when needed) what I don't understand is how to dynamicly create a new instance of a thread, with access to the socket of the connected client upon a new client connection. Each thread would need access to the clients socket on the lNet TCP component and each thread would need to be able to run a procedure based on the events that the socket assigned to that thread fires. Thanks for your quick reply, it's greatly appreciated. Any more help you can give will be amazing. Thanks. Logic~ ---------------------------------------------------------------------- eny · 2011-08-24 21:05 In the main program you monitor a port constantly (I chose Synapse so I used the TTCPBlockSocket for that). As soon as you receive a new request you accept it by instantiating a client specific socket (again, in my case I used something like 'ClientSocket := socket.accept;' ClientSocket is a TSocket object). This client specific socket is going to be one of the attributes of the new thread you need to create: dynamically craete the new thread, give it this socket as one of its attributes and in the thread's Execute method, start processing all request that come in via the client's socket. While this dynamically created thread is handling the client requests, your main program continues to monitor the port for new incoming requests. Finally, if you wish your program to be respsonsive all the time, you could also put the code that monitors the port for incoming connections in a separate thread. The sky is the limit... ---------------------------------------------------------------------- Almindor · 2011-08-24 21:14 You can just use the TLThreadedEventer and you get a nice dynamic threadpool event watching It's a bit experimental atm. tho