I'm using kryonet for a small multiplayer game I've been working on. It's been working fine until I tried to create a runnable jar with gradle's "desktop:dist" command. The command works but the runnable jar doesn't connect to the server properly. When reading the server's output I can see that their is some sort of connection made because the servers disconnect() function is called when the client end times out. Again, if I just compile and run with "desktop:run" everything works as excpected.
I'm stuck, and this is the first networking I've tried with java. Has anyone experienced something similar? The only quasi related errors I could find through google said binding and connecting to both UDP and TCP on the client/server would fix the problem. My code already connects to both.
Someone save me!
colby@monkeybuckets ~/attempt $ sudo ./gradlew desktop:dist
Configuration on demand is an incubating feature.
:core:compileJava UP-TO-DATE
:core:processResources UP-TO-DATE
:core:classes UP-TO-DATE
:core:jar UP-TO-DATE
:desktop:compileJava UP-TO-DATE
:desktop:processResources UP-TO-DATE
:desktop:classes UP-TO-DATE
:desktop:dist
BUILD SUCCESSFUL
Total time: 6.612 secs
package com.evergeen.attempt;
import java.util.*;
import com.esotericsoftware.kryonet.*;
import com.esotericsoftware.kryo.Kryo;
public class GameServer
{
Server server;
Kryo kryo;
LinkedList<ServerPlayer> players;
LinkedList<Connection> connections;
PlayerUpdateRequest updateRequest;
PlayerUpdatePack updatePack;
NewPlayerPack npp;
LinkedList<String> chat;
public static void main(String [] args)
{
GameServer gameServer = new GameServer();
gameServer.runServer();
}
public GameServer()
{
System.out.println("Instance Created...");
connections = new LinkedList<Connection>();
chat = new LinkedList<String>();
}
public ServerPlayer playerWithId(int id)
{
for (ServerPlayer player: players)
if(player.id == id)
return player;
System.out.println("No player with that ID");
return null;
}
public boolean runServer()
{
server = new Server();
kryo = server.getKryo();
kryo.register(PosPack.class);
kryo.register(PCP.class);
kryo.register(PlayerUpdateRequest.class);
kryo.register(PlayerUpdatePack.class);
kryo.register(NewPlayerPack.class);
kryo.register(DCPack.class);
kryo.register(ChatPack.class);
server.start();
players = new LinkedList<ServerPlayer>();
try
{
server.bind(5500, 6500);
System.out.println("Server Running...");
}
catch (Exception e)
{
System.out.println("Couldn't bind to port tard ass");
System.exit(0);
}
server.addListener(new Listener()
{
public void received(Connection connection, Object object)
{
//System.out.println("Recieved packet...");
if (object instanceof ChatPack)
{
System.out.println("Recieved chat pack from: " + connection.getID());
ChatPack pack = (ChatPack) object;
pack.id = connection.getID();
chat.add(pack.line);
//connection.sendTCP(pack);
//Let all players know that isn't self
for (ServerPlayer player: players)
//if(player.getConnection().getID() != connection.getID())
player.getConnection().sendTCP(pack);
}
if (object instanceof PosPack)
{
PosPack pos = (PosPack) object;
System.out.println("Player with ID: " + connection.getID() + " is moving.");
playerWithId(connection.getID()).updatePos(pos);
pos.id = connection.getID();
for (ServerPlayer player: players)
if (player.getID() != connection.getID())
{
System.out.println("Sending movement update...");
player.getConnection().sendUDP(pos);
}
}
if (object instanceof PlayerUpdateRequest)
{
System.out.println("Responding to update request from: " + connection.getID());
//updatePack = new PlayerUpdatePack();
//updatePack.connectedPlayerCount = players.size();
//connection.sendTCP(updatePack);
for (ServerPlayer player: players)
{
if (player.id != connection.getID())
{
NewPlayerPack pack = new NewPlayerPack();
pack.id = player.id;
pack.x = player.x;
pack.y = player.y;
System.out.println("Sending new player pack to: " + player.getConnection().getID());
player.getConnection().sendTCP(new NewPlayerPack());
}
}
}
}
});
server.addListener(new Listener() {
public void connected(Connection connection)
{
//connection.sendTCP(new float[] {5.0f, 6.0f, 7.0f, 8.0f});
System.out.println("Player connecting to server...");
ServerPlayer newPlayer = new ServerPlayer(connection);
newPlayer.id = connection.getID();
System.out.println("New player's ID is: " + newPlayer.id);
players.add(newPlayer);
//connections.add(connection);
//notify players of new connection
for (ServerPlayer player: players)
{
if (player.getID() != newPlayer.id)
{
System.out.println("Sending NPP to " + player.getID());
npp = new NewPlayerPack();
npp.id = newPlayer.id;
npp.x = newPlayer.x;
npp.y = newPlayer.y;
player.getConnection().sendTCP(npp);
}
}
for (ServerPlayer player: players)
{
if (player.getID() != newPlayer.id)
{
System.out.println("Packing player: " + player.getID());
npp = new NewPlayerPack();
npp.id = player.id;
npp.x = player.x;
npp.y = player.y;
connection.sendTCP(npp);
System.out.println("Sent NewPlayerPack(ID,X,Y): " + npp.id + "," + npp.x + "," + npp.y);
}
}
System.out.println("Player connected to server with ID: " + newPlayer.getID());
}
});
server.addListener(new Listener() {
public void disconnected(Connection connection)
{
System.out.println("A player has left the server");
int id = -20;
for (ServerPlayer player: players)
if (player.id == connection.getID())
{
id = player.getID();
players.remove(player);
}
for (ServerPlayer player: players)
{
DCPack dc = new DCPack();
dc.id = id;
player.getConnection().sendTCP(dc);
}
}
});
return true;
}
}