Ive added some networking to a 2D game of mine and basically read a simple serversocket and socket tutorial and just went from there without any other knowledge, i just wanted to make sure im not doing it a really long badly drawn out way.
The server loops round with the ol’ somesocket = serversocket.accept(); in a while loop waiting for clients, once its caught one it stores it in a static array of sockets then creates a new thread to communicate with the client.
This Thread then creates a BufferedReader and PrintStream for the socket that was passed to it sends the map data to client once then for the rest of its life it then just listens for x,y of player to be sent to that socket and reads it when it is and stores it in a static array of xy integers in another class(the one this all started from).
In the main server Thread its in a while loop which sleeps 12 milli everytime in this while loop it repaints the window to display some info, its a JFrame btw and also calls a sendToAll() meathod which sends all xy’s, out to all players using the static arrays of xy cords and sockets mentioned earlier, it has to create a new printstream everytime it does this.
Client side when the game loads the static meathod load in the loadMap class is called which creates a new static socket and trys to connect to the server, then it creates a bufferedReader and gets ye olde map. Then the game continues running.
Everytime player.move() is called in the main gameLoop it creates a new threads which creates a printstream from the static socket in the loadMap class and sends the xy to server.
Then every time the gameloops a getPlayerPostitions meathod is called directly from the loop which gets the x,y’s of all players.
Are the static sockets a good thing? should i be doing this a diffrent way? seems to be working atm and not lagging but thats on local host and i havent actually added anything to the clients yet to draw the other players with the recived x, y’s.
tl;dr good server architecture to 2D game