My game is based on the famous sim-eth-es example and works pretty fine for multiplayer mode. But I also would like to have a single player mode. Currently single player mode also fires up a game server and connect to it. But for example find a free port is already a problem (I need more than one port).
Do someone have a simple and elegant solution for this? Or is it the solution to have a local game server for single player mode as well?!
In the long run, this is the best solution.
For single player games, just pick a super high port and see if it’s available. I keep meaning to add support for ephemeral ports to SpiderMonkey and that may happen soon… for exactly the case of local only games. In that case, it’s also best to bind to 127.0.0.1 to avoid external things hopping into your game unexpectedly.
Pretty much all mtiplayer games use a local server and connect to it, hence games like ARK are playable as multiplayer on low spec machines because they are only running a client. It will definitely make your life easier too, because there is no differentiation between modes.
Until some of your users has a Canon printer or whatever that uses exactly that port for some reason and both your game and the printer go bananas
Remember to put some consistency test, and discard invalid data!
I may be wrong, but I think if you assign the port to zero it will automatically assign an available port, to which you can use .getPort() to discover the value.
There is a way to get the network stack to assign an ephemeral port for you. I’m just not sure that SpiderMonkey makes that easy when you want to add additional channels.
I will be trying it myself soon, though… because SpaceBugs needs this really soon. If I have time to work on it this weekend then I plan to do it. It will make testing 100x easier if I don’t have to walk through the host setup + connect + join every time.