Multiplayer and Game-Server. Where to start?

So, I’ve decided before I start working on my next game that it will have multiplayer capabilities. There will be a server which the client connects to. The client will connect to the server almost constantly, and will handle things like rendering and user input while the server handles everything else. This includes (but is not limited to) entity location, map models/textures to be downloaded, player location/items/health/etc, and one server-tick will be every 1/16th of a second. However, I ran into an issue in the very beginning…

The server waits for a client to connect and request information before continuing through its loop to do everything else. I tried to fix this by using a ServerSocketChannel with configureBlocking set to false instead of using a ServerSocket, however this requires information to be sent/received through the use of ByteBuffers. This wouldn’t be an issue, however I won’t always know the exact amount of information and bytes that will be sent/recieved, so they don’t really work out too well. I’ve been thinking I could try multi-threading, however I’ve never done this before.

tl;dr My questions shortened and paraphrased:

How do I get my server to loop through and handle game-logic while accepting user input at the same time? Should I use multi-threading?
How would YOU write a game-server? (for example, how would you handle client requesting information/server sending information?)

I am completely new to the world of networking. I’ve been programming for years yet never gotten into it. Sorry if I sound completely new to all this, because I am! :slight_smile:

Multithreading is a MUST when it comes to networking. The server must handle a bunch of connections, so thread blocking is a large concern when working with them. If you’ve been programming for years, then you really should know how threading works, it’s one of the basic things you just need to know. Multiple threads would solve your problem, there is really no other way to get around it.

I would use a networking library, though, such as Kryonet. Rolling your own networking library is not something you should really attempt unless the couple out there don’t fit your needs.

You’ve been programming for years but don’t know how to use multi-threading?..

You’ll need to multi-thread… one thread should contain the game loop and all the server logic, and another thread should be handling specifically the networking.

As you receive packets from clients, simply verify the contents of the packet to make sure they are valid, and then modify your game world as needed.

For future reference, networking generally should ALWAYS be handled on a seperate thread.