Kryonet Fails about sending packet

Greetings.

I was setting up Kryonet, but ran into a problem. The connection is successful, but when a packet is sent (either custom or the TCP KeepAlive), it fails.

~file with client code

client.addListener(new ClientListener());
client.start();
client.connect(5000, ip, port);

~file with server code
server.bind(port);
server.addListener(new ServerListener());
server.start();

It connects correctly (it calls the connected listener on both ends). However, when the first packet is sent, it fails. This is true whether it is a registered “custom” type, or the default TCP KeepAlive. The moment it is sent, the client throws.

java.net.SocketException: Connection is closed.
at com.esotericsoftware.kryonet.TcpConnection.readObject(TcpConnection.java:109)
at com.esotericsoftware.kryonet.Client.update(Client.java:247)
at com.esotericsoftware.kryonet.Client.run(Client.java:333)

This happens after 8 seconds - the default KeepAlive setting. If that is disabled (or set over the other default, 12), it gets disconnected as expected after 12 seconds. If the keep alive is set smaller, or a custom packet is sent (after being registered), it throws at that time instead.

The server shows no indication of a problem.

What might be the issue?

Can you show us the full code?

Also, wrap your code in [code ][/code ] tags (without the spaces) please!

I made a SSCCE (I thought it was isolated enough before, and it was in a way, but still probably should have started with it) to test. And, to my ever-continual horror, it worked out of the box. Apparently when transcribing I didn’t copy it exactly.



package sscce;

import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;
import java.io.IOException;
import java.net.InetAddress;


public class SSCCE
{
    public static void main(String[] args)
    {
        Thread serverThread = new Thread(new ServerThread());
        Thread clientThread = new Thread(new ClientThread());
        
        serverThread.start();
        clientThread.start();
    }
}

class ClientListener extends Listener
{
    @Override
    public void connected(Connection connection)
    {
        System.out.println("[Client] Connected.");
    }
    
    @Override
    public void disconnected(Connection connection)
    {
        System.out.println("[Client] Disconnected");
    }
    
    @Override
    public void received(Connection connection, Object object)
    {
        System.out.println("[Client] Received message of type: " + object.getClass() + ".");
    }
}

class ServerListener extends Listener
{
    @Override
    public void connected(Connection connection)
    {
        System.out.println("[Server] Connected.");
    }
    
    @Override
    public void disconnected(Connection connection)
    {
        System.out.println("[Server] Disconnected");
    }
    
    @Override
    public void received(Connection connection, Object object)
    {
        System.out.println("[Server] Received message of type: " + object.getClass() + ".");
    }
}

class ClientThread implements Runnable
{
    @Override
    public void run()
    {
        try
        {
            Client client = new Client();

            client.addListener(new ClientListener());

            client.start();

            client.connect(5000, InetAddress.getLoopbackAddress(), 11111);
        }
        
        catch (IOException ex)
        {
            throw new RuntimeException(ex);
        }
    }
}

class ServerThread implements Runnable
{
    
    @Override
    public void run()
    {
        try
        {
            Server server = new Server();

            server.bind(11111); //works
            //server.bind(11111, 11111);//does not work

            server.addListener(new ServerListener());

            server.start();
        }
        
        catch (IOException ex)
        {
            throw new RuntimeException(ex);
        }
        
    }
}

I had dropped the UDP port in server.bind from before, which made it work. Adding it back in makes it not work. I can’t find any information to suggest why that is, but it seems this is where the problem originates.

EDIT: After looking into this further, it appears I also missed that client.connect also has a UDP port overload as well. It appears the problem is when one end specifies TCP and UDP, and the other is only TCP. When they match, it works. It seems to be working as expected, this having been done. It seems strange it failed rather silently, though.