Introduction to Network multiplayer game in Java

Hello Friends, i want to know the best option i have to consider at the moment of make a online game, i mean, why do i have to know apart of:

TCP/UDP, networks, sockets, client, servers, etc, etc…
i want to focus only in a game, i have been reading a lots of tutorials but i still dont know how to move the player from the client to the server, o how to change the animation from one to another, i have a lot of information in my mind but i still dont link it.

so, there is anybody who know how to make it step by step, 1, and then 2 and then 3, im so confused about it…

thanks in advance, any link to tutorial, or article will be good received…

also i have been looking in this forum but the links are obsoletes and a few ones are working.

For realtime games, forget TCP.

games like?

i dunno, i want to make some experiments, i want to make an MO first, or have a server with 4 players online ( me, and other 3), play a level , thanks for the link

If you’ve no networking experience, then do a turn-based game first with TCP or better yet some library. I’ll let someone else make suggestions in that department.

I’m learning Java for 5 months and my experience is increasing by 10000 every day by trying to create a network game.

Try to watch some videos like this: https://www.youtube.com/watch?v=orVj-VcBZMk

Is was perfect for me, i’ve started from drawing balls, made collisions by myself, then I created a cannon that was throwing the balls, added health, wrote some algorithms - it teaches ALOT.

Then, when you’ll learn how to draw bitmaps on canvas, know what is SurfaceView you can try to implement a simple server/client from Kryonet - it’s E A S Y. And try to do everything on TCP first (it’s slower, but it works smoothly on LAN) - switching on UDP is easy, but you have to learn first, how things work.

i have enough experience in java some years, but i was in web development and using a lot of frames, i want to know what is behind curtains, i already know how to make games in java, the only thing i dont know yet is comunicate diferentes clients from network.

and surfaceview is for android, im using JSE only, and thanks for the link…

The content of your posts in this thread are confusing. Do you want to know what is “behind the curtain” or just focus on the gaming side of things. If you want to make a multiplayer game then you have a two choices, either learn what’s behind the curtain or hire someone else to do it, (I guess there’s a third where you go work for a game studio, but whatever).

That’s not to say you have to reinvent the wheel as, like Roquen said, there are libs out there. It looks like a lot of people here use Kryonet, so at least you’ll have some help if you get stuck. Regardless if you’re going to do the networking you’re going to want to understand how it works so you’re sending the correct information.

maybe i dont explain myself,

let me explain you a little:

i finally know how sockets works but there is IO, NIO and NIO2, how i know wich one i have to use?

and for example, i’ve read that the Servers is the instance of the game that updates the whole game, the client send X e Y coordinates and the servers know where the player is and if it recive a move_left comand, then the server know how to move that player, but the client only render those updates?, the servers only have the logic of the game but not the sprites or the backgrounds?, how to put the animation to the sprite player, in the client, or the server must say to the player wich animation use…

in resume i want to know how the networks works in game oriented,

why i dont use frameworks, because i want to know first whats behind curtains, and i want to make my own class to helpme in those network cases.

i dont like use frameworks if i dont know how it works in the background.

i hope i explain myself well, and i dont speak english, so any correction would be apreciated.

I stand by the “do a turn based game first” statement.

In a client-server model, yes. Client-server is easiest and it’s already pretty hard.

Client sends player’s action. Client is working from last known packet id that the server has ack.

Client is extrapolating from the last set of packets received from server. Since there may have been dropped packets the client has to deal with “smoothing” what the player can see vs. what it knows about state from the most recent server updates.

Like I said…do a turn-based game first. This question make no sense.

Read the links I provided.

A very very rough overview of a possible method is:

Server is running a simulation at fixed time step.
Server is sending out UDP packets at a fixed rate.
Server tracks the last couple of updates each client has acked and the data sent to client is absolute or relative information related to these (packets includes this info).
Client acks any packet it receives assuming it’s new enough that the server can use it for future updates (otherwise neither care about it). It likewise attempts to inform the server of player actions based on last known server acks.

This is a bit like the “start with Pong or Space Invaders” maxim isn’t it?

Never made a networked game before? Then you must first write a chat server. Here is the spec:

  1. Server process listens for incoming connections from clients.
  2. Clients connect to server and introduces itself with a username.
  3. A client types in their own window, and the message is broadcast to all other clients and displayed, along with the username.

Try that first.

Cas :slight_smile:

thanks, i’ve already made a chat aplication with ServerSockets and Sockets,
im studying about ServerSocketChannels,

but what i want to know is to understand the part that make de server and the part that make the client in a networked game,

supose i want to play mario bros, the server and 1player is my pc, and the 2 player is a friend in another pc, that like first example.

or

we can have a server and 1 & 2 players are clientes connected to that server, but i dont get it the best way to do it, and is almost imposible to look for that kind of information online,

so many of you can say “there is documentation”, but that files are for people already know how to do it, i need a dummie tutorial.

If you first forget about any issues about maliscious clients, cheating, security, online updating your client, and all that, you basically ask the questions:

  • what information is there in the game? (player movements/positions, events, …)
  • what assets are needed to render the game on the client-side? (3D meshes, textures, sounds, …)
  • what information of the game resides on which side?
  • what information needs to pass from one side to the other?

“Does the server need to have the 3D meshes and sound files and textures?”
=> [ ] Yes, [ ] No
(hint: the correct answer is No)
Rendering is part of the client. You package and deploy the client-only resources needed for rendering (video and sound rendering) alongside your client.jar running on the client.

Second question could be: “Which information does the client need to send to the server?”
=> [ ] the rendered output of the client, [ ] the relative movements of the player
(hint: the correct answer is the second one)

However, the concrete information depends on your game, but generally should be what the server should know that the client does, so that it can update the game state and inform other players about it.
In a simple pong game it would be the movement direction of the paddle, in which case the server computes the new paddle positions and sends them to other clients.

As this is just about one of the hardest technical problems in computer science, there ain’t no dummies tutorial :slight_smile:

Cas :slight_smile:

you are right, but there are so many tutorials that say:

firs create a class and then we gonna instanciate a ServerSocket, etc…

but i cant find one for 2d games, im reading an ebook of HTML5, where i suppose they say how to create a multiplayer server, in NODEJS but i think i can translate it to java, later or at least the game logic…

Just wondering, how is networking related to the game’s graphics?

Write a latency port forwarder! That’s what I did :point:

  • XML document that describes which protocols/ports/ranges are forwarded to which destination(s), and with what latency range & packet loss probability.
  • port forwarder configures itself according to the document, and sits in the background buffering & forwarding traffic.

It’s useful in 2 regards:

  • It’ll familiarize you with NIO’s Channels, Selectors, and the necessary threading structure.
  • It’ll be a useful tool for when you come to write a future realtime multiplayer game, and want to emulate different connection conditions. (high latency, high packet loss, highly variable latency, asymmetric latency, etc etc)

If you know to make a chat server like you said, then you already have all the tools necessary to make a multiplayer game - which all boils down to sharing information between two (or more) programs. In other words, Networking.

Now it’s generally up to you to decide the whats and hows concerning the sharing of information. This is the meat of multiplayer networking. Sharing and organizing the necessary information to achieve your desired effect. It isn’t easy and can get messy very quickly.

Fortunately this isn’t really a new problem and there are a few popular models that suit most cases concerning game development. Perhaps overkill for a simple game. Read more about these here.

SocketChannels etc are just are just another way of doing networking, i.e. sharing information, and thus doesn’t hold within themselves any magical powers to do multiplayer in games.