Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
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] Disconnecting?  (Read 3531 times)
0 Members and 1 Guest are viewing this topic.
Offline GNecro1
« Posted 2014-08-06 17:03:42 »

Well after i connect to the server and start my game everything is fine but my client disconnects after some time! Why is it doing so?

Java freak! Cheesy
Offline tkausl
« Reply #1 - Posted 2014-08-06 17:09:36 »

Are you sending some sort of KeepAlive-packet? Im almost sure, kryonet should send them automatically, but im not this sure if kryonet does this in both directions.

My English isnt that great. Correct me, if you want, im still learning this Language Smiley
Offline opiop65

JGO Kernel


Medals: 156
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #2 - Posted 2014-08-06 17:11:47 »

You know we really can't help unless you post code. We can't magically diagnose a problem if we have no code to work off of. Your two sentence OP provides no technical details about what happened or how your program is set up.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline trollwarrior1
« Reply #3 - Posted 2014-08-06 17:31:20 »

He is just asking for suggestion. Maybe someone has encountered similar problem and might give him their experience.
Offline GNecro1
« Reply #4 - Posted 2014-08-07 00:17:55 »

client sided network
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  
package net.main;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JOptionPane;

import net.packets.PlayerAddPacket;
import net.packets.PlayerRemovePacket;
import net.packets.PlayerXUpdatePacket;
import net.packets.PlayerYUpdatePacket;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;

public class Network extends Listener {

   private String ip;
   private int port;
   public Client c;
   private Game game;

   public Network(String ip, int port,Game game) {
      this.port = port;
      this.ip = ip;
      this.game = game;
   }

   public void start() {
      c = new Client();
      c.addListener(this);
      Kryo kryo = c.getKryo();
      kryo.register(PlayerAddPacket.class);
      kryo.register(PlayerRemovePacket.class);
      kryo.register(PlayerXUpdatePacket.class);
      kryo.register(PlayerYUpdatePacket.class);
      c.start();
      try {
         c.connect(10000, ip, port);
      } catch (IOException e) {
         e.printStackTrace();
         reconect();
      } finally {
         System.out.println("Status : " + c.isConnected());
      }
   }
   
   public void sendKeepAlive(int time){
      c.setKeepAliveTCP(time);
   }
   
   public void checkConnection(){
      if(!c.isConnected()){
         try {
            start();
         } catch (Exception e) {
            e.printStackTrace();
         }
      }
   }

   private void reconect() {
      try {
         c.reconnect();
      } catch (IOException e) {
         e.printStackTrace();
         String i = JOptionPane.showInputDialog(null, "Cannot connect to : " + ip + port + " will exit!", 1);
         System.exit(0);
      }
   }

   public void received(Connection c, Object o) {
      if (o instanceof PlayerAddPacket) {
         PlayerAddPacket p = (PlayerAddPacket) o;
         MPPlayer mmp = new MPPlayer(p.x, p.y, p.id, p.name);

         game.players.put(p.id, mmp);
      }
      if (o instanceof PlayerXUpdatePacket) {
         PlayerXUpdatePacket p = (PlayerXUpdatePacket) o;
         game.players.get(p.id).x = p.x;
      }
      if (o instanceof PlayerYUpdatePacket) {
         PlayerYUpdatePacket p = (PlayerYUpdatePacket) o;
         game.players.get(p.id).y = p.y;
      }
      if (o instanceof PlayerRemovePacket) {
         PlayerRemovePacket p = (PlayerRemovePacket) o;
         game.players.remove(p.id);
      }
      c.setKeepAliveTCP(120000);
   }
}

Server side of the code
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  
package net.main;

import java.util.HashMap;
import java.util.Map;

import net.packets.PlayerAddPacket;
import net.packets.PlayerRemovePacket;
import net.packets.PlayerXUpdatePacket;
import net.packets.PlayerYUpdatePacket;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;

public class Network extends Listener {

   private int port, maxP;
   public Server server;
   public Map<Integer, Player> players = new HashMap<Integer, Player>();

   public Network(Properties p) {
      port = p.port;
      maxP = p.maxP;
   }

   public void start() {
      server = new Server();
      server.addListener(this);
      ServerGame.logger.info("Server created!");
      Kryo kryo = server.getKryo();
      kryo.register(PlayerAddPacket.class);
      kryo.register(PlayerRemovePacket.class);
      kryo.register(PlayerXUpdatePacket.class);
      kryo.register(PlayerYUpdatePacket.class);
      ServerGame.logger.info("Registered classes!");
      ServerGame.logger.info("Starting server....");
      try {
         server.bind(port);
         server.start();
         ServerGame.logger.info("Done! Server is operational!");
      } catch (Exception e) {
         ServerGame.logger.severe("Something went wrong with server!");
         e.printStackTrace();
      }
   }

   public void connected(Connection c) {
      // if (server.getConnections().length < maxP) {
      // c.setKeepAliveTCP(0);
      // ServerGame.logger.info("We receved connection ID:" + c.getID() +
      // " IP:" + c.getRemoteAddressTCP());
      // ServerGame.logger.info("Disconected, full server!");
      // } else {
      ServerGame.logger.info("We recs connection ID:" + c.getID() + " IP:" + c.getRemoteAddressTCP());
      for (Player p : players.values()) {
         PlayerAddPacket pack = new PlayerAddPacket();
         pack.x = p.x;
         pack.y = p.y;
         pack.name = p.name;
         pack.id = p.id;
         c.sendTCP(pack);
      }
      c.setKeepAliveTCP(120000);
      // }
   }

   public void setKeepAlive(int time) {
      for (Connection c : server.getConnections()) {
         c.setKeepAliveTCP(time * 100);
      }
   }

   public void disconnected(Connection c) {
      ServerGame.logger.info("We were disconected from ID:" + c.getID() + " IP:" + c.getRemoteAddressTCP());
      PlayerRemovePacket p = new PlayerRemovePacket();
      p.id = players.get(c.getID()).id;
      server.sendToAllExceptTCP(c.getID(), p);
   }

   public void received(Connection c, Object o) {
      if (o instanceof PlayerAddPacket) {
         PlayerAddPacket p = (PlayerAddPacket) o;
         Player p2 = new Player();
         p2.x = p.x;
         p2.y = p.y;
         p2.id = p.id;
         p2.name = p.name;
         p2.c = c;
         players.put(c.getID(), p2);
         server.sendToAllExceptTCP(c.getID(), p);
         ServerGame.logger.info("A player joined the game: " + p2.name);
      }
      if (o instanceof PlayerXUpdatePacket) {
         PlayerXUpdatePacket p = (PlayerXUpdatePacket) o;
         if (players.get(c.getID()) != null) {
            players.get(c.getID()).x = p.x;
            server.sendToAllExceptTCP(c.getID(), p);
         }
      }
      if (o instanceof PlayerYUpdatePacket) {
         PlayerYUpdatePacket p = (PlayerYUpdatePacket) o;
         if (players.get(c.getID()) != null) {
            players.get(c.getID()).y = p.y;
            server.sendToAllExceptTCP(c.getID(), p);
         }
      }
      if (o instanceof PlayerRemovePacket) {
         PlayerRemovePacket p = (PlayerRemovePacket) o;
         players.remove(players.get(players.get(c.getID())));
         server.sendToAllExceptTCP(c.getID(), p);
      }
      c.setKeepAliveTCP(120000);
   }
}

Java freak! Cheesy
Offline seismic
« Reply #5 - Posted 2014-08-07 06:28:33 »

Well how often do you send update packets? I found that when I update *too much* (like every frame) I get disconnected too because something cannot keep up. Try to throttle how much you send maybe?

Other than that the code looks okay I guess. I mean it should work Tongue
Offline GNecro1
« Reply #6 - Posted 2014-08-07 06:50:36 »

I am updating as the changes happen and am not continually sending data. And i have tryed to limit it on 0.1 and it sends every 0.1 s x and y and on every change but not working still disconecting!
here is all of the code for server and client

Java freak! Cheesy
Offline GNecro1
« Reply #7 - Posted 2014-08-07 21:14:14 »

Some times it doesn't disconnect, i don't even know what i did.... xD

Java freak! Cheesy
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

Longarmx (49 views)
2014-10-17 03:59:02

Norakomi (38 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (35 views)
2014-10-15 16:18:58

TehJavaDev (65 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (55 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
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!