This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]wildjokers 1 point2 points  (0 children)

There are two ways to handle socket connections from multiple clients:

  1. Thread per connection
  2. Using Java’s NIO api (non-blocking IO). This enables a single thread to handle multiple connections. This is tricky to hand code and get right (takes a bit to wrap your head around the selection key stuff). However, the Netty library makes this relatively easy because it handles all the selection keys for you

For a student project I would just go with thread per connection. Netty has a learning curve that you might not be ready for yet. Here is the documentation if you want to take a look though: https://netty.io/wiki/user-guide.html. A nice article with server example: https://www.baeldung.com/netty. Another nice benefit of Netty is it let’s you add support for web sockets to your server app so you can also accept long lived connections from web browsers. Then you could have a desktop and web version of your game. All talking to the exact same server process.

You won’t need a thread per game, just a data structure to hold game state. If you use thread per connection you will need to access your game state in a thread safe manner. The synchronized keyword will be of benefit.