Multiplayer Using Pure Java

Hello Everyone,

Right now i want to start getting into Networking in pure java. I’d like to be able to create a 2d platformer that can connects to a server and can be joined by multiple clients. The problem is. I don’t know where to start. I can make the platformer no problem but when it comes to networking i’m stuck. Would anyone have any good material i could maybe look at or maybe some code i could try to read just to figure it all out. I can set up a basic enough server where it has a client connect to it and it sends a message back from the server. But i don’t know where to go from there.

Maybe someone with some knowledge in this area could help. Id like to be able to do it with no 3rd party libraries also.

Thanks
-GlennBrann

It sounds like you have the basics there already and so need to now think about:

  1. What sort of data messages to be sent? i.e. Deltas or complete game states, events (keyboard/Mouse? )
  2. frequency of messages. strategy games can be infrequent while fps need more frequent
  3. LAN or via internet

Glenn, first thing i would tell you to do is to create a little chat.
Client 1 -> server <- n.Clients

That way you can check how it works :slight_smile: I made one so i can help you. But you need start with something.

You know what i was thinking the same thing. I guess i probably should. Thanks

Scroll down until the title “Multiplayer Networking” http://hub.jmonkeyengine.org/wiki/doku.php/jme3. It is a in-depth tutorial about networking in Java.

You can do several things with the resources on this link:

  • You can study them since they are written entirely in Java.

  • You can use the JMonkey engine for easy implementation of this material.

  • You can take those libraries and make them work for your game.

Some tips you might need :

When you sending some data to the user, you might want to tell the client what type of info you are sending.
Is it a Message? Is it a Ban Message? Is it a Invite to a new Room?

So, before every message, you could have, like a integer,
If its 0 , its a message.
If its 1, its …

Heres a little example
Got from my old chat Code… Bring memories :stuck_out_tongue:

  public void errorMessage(String message,Socket socket)
    {
        try {
           
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(bos);
            dos.writeByte(7);
            dos.writeUTF(message);
            byte[] msg = bos.toByteArray();

            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            out.writeInt(msg.length);

            //System.out.println("@Encoder/Server/Tamanho da mensagem Sendo Enviada : " + msg.length);

            out.write(msg);
            out.flush();

            //System.out.println("@Encoder/Server/Bytes Enviados para " + socket.getInetAddress().getHostName());

        } catch (Exception e) {
            System.out.println("Error in Transmitter Class! (Connection lost ?)" + e.getMessage());
            //e.printStackTrace();
        }  
    }
    
    
    
    
    //MessageTypeZero ::
    public void defaultBlackMessage(String message, Socket socket) {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(bos);
            dos.writeByte(0);
            dos.writeUTF(message);
            byte[] msg = bos.toByteArray();

            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            out.writeInt(msg.length);

            //System.out.println("@Encoder/Server/Tamanho da mensagem Sendo Enviada : " + msg.length);

            out.write(msg);
            out.flush();

            //System.out.println("@Encoder/Server/Bytes Enviados para " + socket.getInetAddress().getHostName());

        } catch (Exception e) {
            //System.out.println("Error in Transmitter Class!");
            //e.printStackTrace();
        }
    }

Why are you creating buffer streams every time you send a packet? That’s probably not very efficient at all when sending thousands of packets per second…

Why not just have one Stream and have it open for the entire time the program is running?

Was some time ago, i didnt have knowledge about memory stuff etc;; Today would be very different.

check out vanzeebans tutorials he covers this in java. his youtube is designs by zephyr.