Why Threads for the Client on the Server?

Hey, I am just playing around a little bit, so no Code here. But I am interested in how to do this.
I knew there are libs like Kryonet, but I would like to knew how to do it.

When I am checking some Online Game Sources I nearly always see that the Server is listening for CLients and for each(sic!) one it makes an own Thread… Why?

Afaik, if I have a lot of Threads ~double the amount of my Cores the PC´s Power goes down extremely. So why do some people do that? Wouldn´t it be better to make some Systems eg.: Movement, Battle, Chat System a own Thread? Although, I could think that with that the sync between Movement and Battle could be a little bit annoying :confused:
Or instead for every Client an Thread I could “batch” some Client´s together and do the work on those. But for a lot of CLient´s I will get again the same Problem.

Why not just accept the incoming login and add this Client into a list and iterate over it? I doubt that it would be an recognizable time difference, especially at RPG´s with <100 current Players. On the other Hand for Strategy Games that could be too slow.

Anyone can clarify/sort my thoughts?

Have a read up on blocking and non-blocking I/O.

Back in 2009 Ran Dahl addressed that same issue in his presentation on Node.js.

He explained how it can be solved with an event loop and asynchronous code.
Java has a few classes built in that can be used to get the same effect. e.g. AsynchronousServerSocketChannel, ExecutorService and CompletableFuture.
Regarding your idea of a list - the ExecutorService class has a queue built in that schedules all the tasks that still need to be done but it all works more efficiently than what you were describing. That built in queue corresponds to the event loop.

Okay thanks.

What would you now suggest how to implement it on the Serverside? Assuming <100 Clients.
Just one Thread per Client or something else? Better use a CachedThreadpool for that?

You will be very good off using a networking library like Kryonet or even XNIO or Netty.
Writing a working, error-free, non-blocking network library is a huge task, should you decide to go non-blocking (one or a few threads per server, and not one-thread-per-client). Kryonet will provide everything you need. XNIO and Netty provide more features, like encryption and FTP/HTTP protocol implementations, usually not needed for games, but their small core with their “pipeline” architecture provides a good way to start (freeing you from handling non-blocking sockets and framing/buffering).

Let’s say you and I are both users. I’ve taken an action that takes a long time (maybe I’m uploading a video). You want to do something that doesn’t take a long time (like refresh the homepage). Should you really have to wait for my video to finish uploading to see the homepage refreshed?

[quote=“theOne,post:4,topic:57615”]
There isn’t one single answer. It’s going to depend on exactly your context and requirements.

But most frameworks handle threading themselves, so the smartest thing to do is probably just let somebody else do the work for you.

Could you be more specific? What exactly are you trying to do?

With less than 100 clients it doesn’t really matter what strategy you use. Even with one thread per client there shouldn’t be any performance issues.

Different context, but related?

I was making and using individual threads for audio event streams where certain sound effects schedule their next playbacks. Going to an ExecutivorService, with a FixedThreadPool instead improved performance. But I only need to run a pool of 10, so far.

Kevin Workman’s explanation is to the point. I remember this coming up in a lecture in college about operating systems back in the 1980’s (I had a work-study job video-taping lectures, I wasn’t actually a CS major), where they made a point about there being drawbacks with strict first-in first-out scheduling.

Well this question was more theoretical. For productive Code I would of course use some well etablied library.
But I like to go from "low " level to learn some things.

And yes, I knew that it is needed to Thread those CLients that other tasks don´t block.
But I am just curious.
When I am looking at some (very simple) Codes they do also use a single Thread for one CLient.
Like… just Chat Handlers or Game Server where you don´t get any complicated Commands.
If a bunch of CLients send to an Server some Texts, what exactly would slow down the Server here? The Server just listens, adds the Texts to an que and send them and the same iteration to the other Clients.

Or when the Server gets an Position packet, “want to attack Monster XYZ”. Server takes those commands and delegates them to (often) non-threaded Systems/Handlers. and this is where the work happens.

So… why do I see so many examples where the Clients are threaded and that´s it?

I only have seen some total beginner examples and Repository Code and in every? single one it was done like that.

btw: Is there an other Version of kryonet? the exotericsoftware/kryonet was been updated three years ago.

it is cos’ they’re outdated.

yet, using NIO it is controverse. i wrote up a bit of “wishfull-thinking” code : http://www.java-gaming.org/topics/managing-when-the-server-should-send-which-data/35591/msg/339093/view.html#msg339093

mind what Riven says, he’s very right.

Yeah… then shall it be still used?

Is there an Condition to send packets over TCP per Kryonet?
always when I try to send one Packet (Class with three Stings) the Client disconnects from the Server.
If I don´t use that Method it says logged into the Server.
I do not get any errors, just a DC.


 @Test
    public void connectToServerTest() {
        LoginServer server = LoginServer.getInstance();
        MyClient client = MyClient.getInstance();

        while (!client.client.isConnected()) {

        }
        client.tryToLogin("test","test");

      while(client.client.isConnected()){
          System.out.println("still connectde");
          try {
              Thread.sleep(400);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }
    }

 public void tryToLogin(String name, String password) {
        Logger.getInstance().log(this, "sending LoginPacket....");
        client.sendTCP(new LoginPacket(name, password, generatedSalt));
    }

If I call tryToLogin() it jumps directly to disconnected() and the connection is null, if I remove that Line it will stay online.

KryoNet does not show you all debug messages by default.
You need to set the log level to get debug information, it’ll tell you how KryoNet responded to you sending the object.
I expect it’s something like object not registered or buffer overflow.

Example:

//Package is com.esotericsoftware.minlog
Log.set(Log.LEVEL_DEBUG)

Call this before you call the suspicious method and disable it (Log.LEVEL_NONE) soon after or the log will be huge and may flood your console!

Nope, already got NotregisteredException and figured all of them out.

Adding the Log Level it does not change.

btw, should I make an own Thread for this?..

You solved it, why make another thread?

KryoNet has 2 “jar versions”, debug and production i think.
The production version will not output debug messages no matter what log level you’re on.