Client threads in a thread-per-client online game

I heard the old paradigm is the new thing, so I abandoned NIO for now.

That is the best practise for Threads on the client, when using standard java Sockets? One thread for UI, and one for networking? Should I just reference the networking Thread in the UI-instance?

How would they communicate? Adding packets to a thread-safe queue for sending to the server, and then looping checking if there is something to be read from the InputStream? Is it okay to have both input and output on the same thread?

How would this networking thread work? I can’t just loop (apparently) because reading from the inputstream will block.

So how is this done? Examples and pseudocode are appriciated :slight_smile:

I don’t have a real answer for you, but I will say that you will save yourself a lot of time and hair if you use a working third party solution like Kryonet.

Thanks for suggestion, but I already have used KryoNet once before. I would like to try using the low-level Sockets and get a feeling for what is going on under the hood.

Well then I direct you to the tutorial series on game networking by Glenn Fiedler: http://gafferongames.com/networking-for-game-programmers/
Actually I guess it’s not really a series, so just read them in any order you like, but defiantly read all of them, they’re pretty great IMO.

Since I’m having trouble with Threads using blocking IO with Swing, I’m jumping to LibGDX with Kryonet. I have one question however.

Is a single thread enough for both the clients networking and it’s UI updates? I know this is not possible with blocking IO, but is Kryonet sufficiently fast for my framerate not to drop dramatically?

If your networking code has a performance impact at all you’re doing something VERY wrong. Any time proven library will be fast enough. Unless you’re creating a server for hundreds of simultaneous players, your choice of library should only be based on convenience.

It would be a slight learning curve, but Akka (http://akka.io) would change your life forever when dealing with threading, and it has networking built in. I use it on a large server multiplayer engine, but it’s light enough to use on the client. It basically gives you a high level abstraction for threading and networking that is just better then anything else currently out there in java.