Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Multiplayer Using Pure Java  (Read 502 times)
0 Members and 1 Guest are viewing this topic.
Offline GlennBrann
« Posted 2013-12-29 20:05:40 »

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
Offline moogie

JGO Ninja


Medals: 15
Projects: 6
Exp: 10 years


Java games rock!


« Reply #1 - Posted 2013-12-30 01:50:00 »

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


Java4k RIP 2014
Offline Andre Lopes
« Reply #2 - Posted 2013-12-30 01:52:28 »

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

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 Smiley I made one so i can help you. But you need start with something.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GlennBrann
« Reply #3 - Posted 2013-12-30 01:53:42 »

You know what i was thinking the same thing. I guess i probably should. Thanks
Offline PixelappGames
« Reply #4 - Posted 2013-12-30 01:56:55 »

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.

Cloud-games and fun.
Offline Andre Lopes
« Reply #5 - Posted 2013-12-30 01:58:04 »

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 Tongue

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
  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();
        }
    }
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #6 - Posted 2013-12-30 03:03:58 »

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...

Offline tyeeeee1
« Reply #7 - Posted 2013-12-30 03:21:23 »

Why not just have one Stream and have it open for the entire time the program is running?
Offline Andre Lopes
« Reply #8 - Posted 2013-12-31 02:04:30 »

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.
Offline lcass
« Reply #9 - Posted 2013-12-31 02:36:31 »

check out vanzeebans tutorials he covers this in java.  his youtube is designs by zephyr.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (8 views)
2014-12-27 22:38:51

Mr.CodeIt (13 views)
2014-12-27 04:03:04

TheDudeFromCI (17 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (56 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (92 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (86 views)
2014-12-05 12:39:02

SHC (97 views)
2014-12-03 16:27:13
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!