🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Kryonet not registering update

Started by
-1 comments, last by mrbovinejony 7 years, 7 months ago

First off, I am new to network programming and the code I have is mostly copy/paste from a tutorial I found. I've looked at this for a few days and I can't find whats wrong and I don't even know where to start when it comes to debugging network issues.

I am using 2 clients on the same computer while testing this. When I open the second client, two players appear on the screen as expected. But any movement or player update is not sent to the other client so I'm curious if a packet is not being sent to the right place.


public class PlayerClient extends Listener{
    public Client client;
    String ip = "localhost";
    int port = 27960;

    public void connect(){
        client = new Client();
        Kryo kryo = client.getKryo();
        kryo.register(Packet.Packet0LoginRequest.class);
        kryo.register(Packet.Packet1LoginAnswer.class);
        kryo.register(Packet.Packet2Message.class);
        kryo.register(Packet.Packet3AddPlayer.class);
        kryo.register(Packet.Packet4RemovePlayer.class);
        kryo.register(Packet.Packet5UpdatePlayer.class);
        kryo.register(Packet.Packet6MovePlayer.class);
        client.addListener(this);

        new Thread(client).start();
        try {
            client.connect(5000, ip, port, port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void received(Connection c, Object o){
        if(o instanceof Packet.Packet3AddPlayer){
            Packet.Packet3AddPlayer packet = (Packet.Packet3AddPlayer) o;
            MPPlayer newPlayer = new MPPlayer();
            MainGame.players.put(packet.id, newPlayer);

        }else if(o instanceof Packet.Packet4RemovePlayer){
            Packet.Packet4RemovePlayer packet = (Packet.Packet4RemovePlayer) o;
            MainGame.players.remove(packet.id);

        }else if(o instanceof Packet.Packet5UpdatePlayer){
            Packet.Packet5UpdatePlayer packet = (Packet.Packet5UpdatePlayer) o;
            MainGame.players.get(packet.name).x = packet.x;
            MainGame.players.get(packet.name).y = packet.y;

        }
    }
}

This is the player client class that is created every time a client is opened on the computer. This class is being added like so:


        startGameButton.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                mainGame.playerClient = new PlayerClient();
                mainGame.playerClient.connect();
                mainGame.SetClassScreen();
            }
        });

A button is clicked and opens a new client, and then switches to the player class chooser screen. In the class chooser screen, there is no network code. Packets are being sent is the PlayScreen class every update, which I know is not ideal but I'm using it for now just for testing purposes.


    private void sendUpdatePacket(){

        Packet.Packet5UpdatePlayer packet = new Packet.Packet5UpdatePlayer();
        packet.x = (int)player.body.getPosition().x;
        packet.y = (int)player.body.getPosition().y;
        MainGame.playerClient.client.sendUDP(packet);
        System.out.println(packet.name);
    }

This method is being called in the update method. The system.out line is just to make sure a packet is being sent. Packet5UpdatePlayer just takes an x and y position of the player. Now the server:


public class StealthServer extends Listener{
    private static Server server;
    private static Map<Integer, ServerPlayer> players = new HashMap<Integer, ServerPlayer>();
    private static int port = 27960;



    public static void main(String[] args) throws IOException{
        server = new Server();
        Kryo kryo = server.getKryo();
        kryo.register(Packet.Packet0LoginRequest.class);
        kryo.register(Packet.Packet1LoginAnswer.class);
        kryo.register(Packet.Packet2Message.class);
        kryo.register(Packet.Packet3AddPlayer.class);
        kryo.register(Packet.Packet4RemovePlayer.class);
        kryo.register(Packet.Packet5UpdatePlayer.class);
        kryo.register(Packet.Packet6MovePlayer.class);
        server.bind(port, port);
        server.start();
        server.addListener(new StealthServer());
        System.out.println("The server is ready");
    }

    public void connected(Connection c){
        ServerPlayer player = new ServerPlayer();
        player.x = 256;
        player.y = 256;
        player.c = c;

        Packet.Packet3AddPlayer packet = new Packet.Packet3AddPlayer();
        packet.id = c.getID();
        server.sendToAllExceptTCP(c.getID(), packet);

        for(ServerPlayer p : players.values()){
            Packet.Packet3AddPlayer packet2 = new Packet.Packet3AddPlayer();
            packet2.id = p.c.getID();
            c.sendTCP(packet2);
        }

        players.put(c.getID(), player);
        System.out.println("Connection received.");
    }

    public void received(Connection c, Object o){
        if(o instanceof Packet.Packet5UpdatePlayer) {
            Packet.Packet5UpdatePlayer packet = (Packet.Packet5UpdatePlayer) o;
            players.get(c.getID()).x = packet.x;
            players.get(c.getID()).y = packet.y;

            packet.name = c.getID();
            server.sendToAllExceptUDP(c.getID(), packet);
            System.out.println("received and sent an update X packet");
        }
    }

    public void disconnected(Connection c){
        players.remove(c.getID());
        Packet.Packet4RemovePlayer packet = new Packet.Packet4RemovePlayer();
        packet.id = c.getID();
        server.sendToAllExceptTCP(c.getID(), packet);
        System.out.println("Connection dropped.");
    }

The connected method works for sure because I can see the second player on my screen when it connects. Player disconnecting and updating are not working and I don't know why. The class ServerPlayer takes an x, y, and Connection value. Last but not least is the spot where the player should be drawn.


        for(MPPlayer player : MainGame.players.values()){
            if (player.getTexture() == null){
                player.setTexture(new Texture("player.png"));
            }
            player.draw(game.batch);
        }

This is in the render method of my PlayScreen class. MPPlayer takes an x, y, and ID plus some other player related stuff to my game.

I know this is long but I can't seem to find where the problem lies. I have a debug line being set direct below this for loop and it shows that MainGame.players.size has nothing in it which leads me to believe that the server is not reading correctly the packet when a player is connected.

If anyone can see anything wrong from first glance or has some debug tips it would be greatly appreciated.

This topic is closed to new replies.

Advertisement