Recent

Author Topic: LNet - Creating a new thread per connected client.  (Read 6553 times)

Logic_Bomb

  • New Member
  • *
  • Posts: 41
LNet - Creating a new thread per connected client.
« on: August 24, 2011, 10:20:01 pm »
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

  • Hero Member
  • *****
  • Posts: 1653
Re: LNet - Creating a new thread per connected client.
« Reply #1 on: August 24, 2011, 10:43:11 pm »
Well, you have already given the answer yourself:

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.
Start by reading the Wiki section about threads and go from there: http://wiki.lazarus.freepascal.org/Multithreaded_Application_Tutorial.

Quote
Any 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  :)
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

Logic_Bomb

  • New Member
  • *
  • Posts: 41
Re: LNet - Creating a new thread per connected client.
« Reply #2 on: August 24, 2011, 10:50:00 pm »
Well, 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

  • Hero Member
  • *****
  • Posts: 1653
Re: LNet - Creating a new thread per connected client.
« Reply #3 on: August 24, 2011, 11:05:29 pm »
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...  8-)
« Last Edit: August 24, 2011, 11:07:26 pm by eny »
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

Almindor

  • Sr. Member
  • ****
  • Posts: 412
    • http://www.astarot.tk
Re: LNet - Creating a new thread per connected client.
« Reply #4 on: August 24, 2011, 11:14:01 pm »
You can just use the TLThreadedEventer and you get a nice dynamic threadpool event watching :)

It's a bit experimental atm. tho

 

TinyPortal © 2005-2018