Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (521)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
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  
  [KryoNet] Networking only works on LAN?  (Read 1879 times)
0 Members and 1 Guest are viewing this topic.
Offline sapake

Junior Newbie





« Posted 2013-10-23 14:51:38 »

Hi everyone.

I'm making a rpg game with plain Java2D and I'm using KryoNet library to do the networking. Now when I test the game on the same computer, everything works fine, clients send data to server and server sends that to other clients. But when I try to test it with my friend who is on other computer the connection fails every time. We have tried Hamachi but it's the same. Do you guys have any similar experiences, fixes or ideas on what is wrong?

Here are my GameClient and GameServer classes (Sorry for messy code, I'm gonna clean it later I promise  Tongue) if they are any help at all.

GameClient
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  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
package net;

import Game.gameManager;
import Gui.Ginterface;
import Gui.UI;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import java.awt.Point;
import java.io.IOException;

public class GameClient {
   
    private Client client = new Client();
    private GamePackage pack;
    private Ginterface inface;
    private gameManager mgm;
    private String username, hostAddress;
    private UI ui;
   
    public void voidSetHostAdd(String a) {
        hostAddress = a;
    }
   
    public void setUi(UI ui) {
        this.ui = ui;
    }
   
    public void setInface(Ginterface inface) {
        this.inface = inface;
    }
   
    public void setMgm(gameManager mgm) {
        this.mgm = mgm;
    }
   
    public void setUsername(String username) {
        this.username = username;
        sendJoinMSG();
    }
   
    public GameClient(String a) {
        hostAddress = a;
        new Thread(client).start();
        Kryo kryo = client.getKryo();
        kryo.register(byte[].class);
        kryo.register(GamePackage.class);
        try {
            client.connect(30000, hostAddress, 54555, 54777);
        } catch (IOException e) {
        }
        client.addListener(new Listener() {
            @Override
            public void received(Connection connection, Object object) {
                if (object instanceof GamePackage) {
                    GamePackage pack = (GamePackage) object;
                    String[] dataArray = readData(pack.getData()).split(",");
                   
                    //Process the incoming packages


                    if ("-1".equals(dataArray[0])) {
                        ui.newNick();
                    }
                    if ("-2".equals(dataArray[0])) {
                        sendJoinMSG();
                    }
                    if ("00".equals(dataArray[0]) && dataArray.length > 1) {
                        inface.addMsg(pack.getSenderName() + ": " + dataArray[1]);
                    }
                    if ("01".equals(dataArray[0])) {
                        if (!pack.getSenderName().equals(username) && !mgm.hasMPP(pack.getSenderName())) {
                            inface.addMsg("> " + pack.getSenderName() + " connected.");
                            mgm.addMPPlayer(pack.getSenderName(), new Point(300, 300));
                            String data = "05";
                            pack = new GamePackage(data.getBytes(), username);
                            client.sendTCP(pack);
                        }
                    }
                    if ("05".equals(dataArray[0])) {
                        if (!pack.getSenderName().equals(username) && !mgm.hasMPP(pack.getSenderName())) {
                            System.out.println("05");
                            inface.addMsg("> " + pack.getSenderName() + " connected.");
                            mgm.addMPPlayer(pack.getSenderName(), new Point(300, 300));
                        }
                    }
                    if ("02".equals(dataArray[0])) {
                        inface.addMsg("> " + pack.getSenderName() + " disconnected.");
                        mgm.removeMPPlayer(pack.getSenderName());
                    }
                    if ("03".equals(dataArray[0]) && dataArray.length >= 2) {
                        if (mgm.hasMPP(pack.getSenderName())) {
                            mgm.getMPP(pack.getSenderName()).setPosition(new Point(Integer.parseInt(dataArray[1]), Integer.parseInt(dataArray[2])));
                        }
                    }
                    if ("07".equals(dataArray[0])) {
                        inface.setIsAdmin(true);
                    }
                    if ("08".equals(dataArray[0])) {
                        inface.setIsAdmin(false);
                    }
                    if ("09".equals(dataArray[0])) {
                        inface.addMsg("You have been kicked from the game.");
                        mgm.cleanAll();
                        sendDisconnect();
                    }
                    if ("26".equals(dataArray[0]) && dataArray.length >= 2) {
                        if (mgm.hasMPP(pack.getSenderName())) {
                            mgm.getMPP(pack.getSenderName()).setPosition(new Point(Integer.parseInt(dataArray[1]), Integer.parseInt(dataArray[2])));
                            mgm.getMPP(pack.getSenderName()).setFrame(Integer.parseInt(dataArray[3]));
                        }
                    }
                }
            }
        });
       
       
       
    }

     //Sending packages
   
    public String readData(byte[] data) {
        String message = new String(data).trim();
        return message;
    }
   
    public void sendMSG(String msg) {
        String data = "00" + "," + msg;
        pack = new GamePackage(data.getBytes(), username);
        client.sendTCP(pack);
    }
   
    public void sendJoinMSG() {
        String data = "01";
        pack = new GamePackage(data.getBytes(), username);
        client.sendTCP(pack);
    }
   
    public void sendDisconnect() {
        String data = "02";
        pack = new GamePackage(data.getBytes(), username);
        client.sendTCP(pack);
        client.close();
    }
   
    public void sendPosT() {
        String data = "03" + "," + ((int) mgm.getPlayer().getPosition().getX()) + "," + ((int) mgm.getPlayer().getPosition().getY());
        pack = new GamePackage(data.getBytes(), username);
        client.sendTCP(pack);
    }
   
    public void subNick(String n) {
        username = n;
        String data = "04";
        pack = new GamePackage(data.getBytes(), username);
        client.sendTCP(pack);
    }
   
    public void sendPrivateMSG(String msg, String to) {
        String data = "06" + "," + to + "," + msg;
        pack = new GamePackage(data.getBytes(), username);
        client.sendTCP(pack);
    }
   
    public void sendAdminTo(String who) {
        String data = "07," + who;
        pack = new GamePackage(data.getBytes(), username);
        client.sendTCP(pack);
    }
   
    public void removeAdminFrom(String who) {
        String data = "08," + who;
        pack = new GamePackage(data.getBytes(), username);
        client.sendTCP(pack);
    }
   
    public void kickPlayer(String who) {
        String data = "09," + who;
        pack = new GamePackage(data.getBytes(), username);
        client.sendTCP(pack);
    }
   
    public void sendPosU() {
        String data = "26" + "," + ((int) mgm.getPlayer().getPosition().getX()) + "," + ((int) mgm.getPlayer().getPosition().getY() + "," + mgm.getPlayer().getPg().getFrames());
        pack = new GamePackage(data.getBytes(), username);
        client.sendUDP(pack);
    }
}


GameServer

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  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
package net;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;
import java.io.IOException;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;

public class GameServer {

    private Server server;
    private static Scanner scan = new Scanner(System.in);
    private HashMap<Connection, String> users = new HashMap<>();
    private HashMap<String, Connection> rev = new HashMap<>();

    public GameServer() {
 

        server = new Server();
        Random rand = new Random();
        new Thread(server).start();
        Kryo kryo = server.getKryo();
        kryo.register(byte[].class);
        kryo.register(GamePackage.class);
        try {
            server.bind(54555, 54777);
        } catch (IOException e) {
        }
        final byte[] someData = new byte[10];
        for (int i = 0; i < 10; i++) {
            someData[i] = (byte) rand.nextInt(5);
        }
        server.addListener(new Listener() {
            @Override
            public void received(Connection connection, Object object) {

                if (object instanceof GamePackage) {

                    GamePackage pack = (GamePackage) object;

                    String[] dataArray = readData(pack.getData()).split(",");

                    if (Integer.parseInt(dataArray[0]) <= 5 && Integer.parseInt(dataArray[0]) > 0) {
                        server.sendToAllExceptTCP(connection.getID(), object);
                    } else if (Integer.parseInt(dataArray[0]) == 0) {
                        server.sendToAllTCP(object);
                    }
                    if (Integer.parseInt(dataArray[0]) == 6 && dataArray.length >= 3) {
                        if (rev.containsKey(dataArray[1])) {
                            GamePackage packN = new GamePackage(("00," + dataArray[2]).getBytes(), pack.getSenderName());
                            server.sendToTCP(rev.get(dataArray[1]).getID(), packN);
                        }
                    }
                    if (Integer.parseInt(dataArray[0]) == 7) {
                        if (rev.containsKey(dataArray[1])) {
                            GamePackage packN = new GamePackage(("07,").getBytes(), pack.getSenderName());
                            server.sendToTCP(rev.get(dataArray[1]).getID(), packN);
                        }
                    }
                    if (Integer.parseInt(dataArray[0]) == 8) {
                        if (rev.containsKey(dataArray[1])) {
                            GamePackage packN = new GamePackage(("08,").getBytes(), pack.getSenderName());
                            server.sendToTCP(rev.get(dataArray[1]).getID(), packN);
                        }
                    }
                    if (Integer.parseInt(dataArray[0]) == 9) {
                        if (rev.containsKey(dataArray[1])) {
                            GamePackage packN = new GamePackage(("09,").getBytes(), pack.getSenderName());
                            server.sendToTCP(rev.get(dataArray[1]).getID(), packN);
                        }
                    }
                    if (Integer.parseInt(dataArray[0]) == 1) {
                        if (users.containsValue(pack.getSenderName())) {
                            pack = new GamePackage("-1".getBytes(), "SERVER");
                            server.sendToTCP(connection.getID(), pack);
                        } else {
                            users.put(connection, pack.getSenderName());
                            rev.put(pack.getSenderName(), connection);
                        }
                    }
                    if (Integer.parseInt(dataArray[0]) == 4) {
                        if (users.containsValue(pack.getSenderName())) {
                            pack = new GamePackage("-2".getBytes(), "SERVER");
                            server.sendToTCP(connection.getID(), pack);
                        } else {
                            users.put(connection, pack.getSenderName());
                            rev.put(pack.getSenderName(), connection);
                            String data = "01";
                            pack = new GamePackage(data.getBytes(), pack.getSenderName());
                            server.sendToAllExceptTCP(connection.getID(), pack);
                        }
                    }
                    if (Integer.parseInt(dataArray[0]) >= 26) {
                        server.sendToAllUDP(object);
                    }
                }
            }
        });
    }

    public String readData(byte[] data) {
        String message = new String(data).trim();
        return message;
    }

    public void sendMSG(String msg) {
    }
}


And the GamePackage class

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
package net;

public class GamePackage {

   private byte[] data;
   private String senderName;
   
   public GamePackage() {
     
   }

   public GamePackage(byte[] data, String name) {
      this.data = data;
      senderName = name;
   }

   public byte[] getData() {
      return data;
   }

   public String getSenderName() {
      return senderName;
   }
}


Offline opiop65

JGO Kernel


Medals: 159
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #1 - Posted 2013-10-23 18:37:40 »

Have you port forwarded your program? I've never used Hamachi so I don't know whats wrong with that.

Offline sapake

Junior Newbie





« Reply #2 - Posted 2013-10-23 18:58:14 »

Have you port forwarded your program? I've never used Hamachi so I don't know whats wrong with that.
Well I quess I should try port forwarding, but Hamachi should work just the same so I'm not sure if that is causing my problem. I'll probably try forwarding tomorrow just to be sure, thanks for answering!  Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 159
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #3 - Posted 2013-10-23 20:59:32 »

No problem Smiley Have you tried running it on local host just to see if it works?
You can always hit the appreciate button on the top right of my post to thank me Wink

Offline Herjan
« Reply #4 - Posted 2013-10-23 21:10:05 »

No problem Smiley Have you tried running it on local host just to see if it works?

Main post:

Quote
Now when I test the game on the same computer, everything works fine.

I'm not sure if both UDP and TCP work over hamachi (no experience), so hopefully port-forwarding works.
Once, I had this problem in C++ using UDP, but that's another (unsolved) story...

Offline opiop65

JGO Kernel


Medals: 159
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #5 - Posted 2013-10-23 21:18:34 »

Isn't Hamachi a VPN? Would UDP have a problem with it sense it doesn't require a handshake before sending packets? Honestly, I don't know if that would affect anything, but maybe that's it. I haven't looked over his code too much, but I know that kryonet doesn't support or advertise their UDP features very widely. So I'd guess OP is using TCP, which shouldn't have a problem with a VPN.

Offline cobolfoo

Junior Newbie





« Reply #6 - Posted 2013-10-30 21:48:28 »

Please make sure Kryonet is not binding on your loop-back device (127.0.0.1).
You can verify with the command netstat in console:

On windows: netstat -an

On Linux: netstat -ln

You could also try to telnet the TCP port directly (with a software like putty on Windows, telnet command line on Linux/OSX).

Lastly, make sure your firewall is not simply blocking your application port, the first time you run your program a popup window asking a confirmation shows up. It only show once Smiley

HAMACHI is like being in a LAN, no need of port forwarding and both UDP and TCP would work (with extra latency).
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.

xFryIx (58 views)
2014-11-13 12:34:49

digdugdiggy (37 views)
2014-11-12 21:11:50

digdugdiggy (30 views)
2014-11-12 21:10:15

digdugdiggy (26 views)
2014-11-12 21:09:33

kovacsa (48 views)
2014-11-07 19:57:14

TehJavaDev (51 views)
2014-11-03 22:04:50

BurntPizza (51 views)
2014-11-03 18:54:52

moogie (66 views)
2014-11-03 06:22:04

CopyableCougar4 (65 views)
2014-11-01 23:36:41

DarkCart (151 views)
2014-11-01 14:51:03
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

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!