Kryonet client disconects from server after sending array

Hey everyone, when my server sends 2d int array to client it stops receiving any more packets.

  static int[][] getMapToSend(Location){
        int[][] a = new int[40][40];
        
        int x1 = 0,y1 = 0;
        
                    for(int i = l.x-20;i<l.x+20;i++){
                        for(int j = l.y-20;j<l.y+20;j++){
                    if(!ServerState.chunkLoaded(l.x, l.y)){
                         a[x1][y1] = ServerState.loadChunk(i, j).tiles[i%100][j%100];
                    }else{
                         a[x1][y1] = ServerState.getChunk(i,j).tiles[i%100][j%100];
                    }
                            
                            x1+=1;
                        }
                        y1+=1;
                        x1=0;
                    }
        
                    
                   
        return a;
    }
    static void sendMap(Player p,Location l,Connection c){  
                    int[][] mapToSend = getMapToSend(l);
                    server.sendToTCP(c.getID(), mapToSend);
    }

Here are the loadChunk and getChunk methods

    
static Chunk[][] map = new Chunk[200][200];

public static Chunk loadChunk(int x,int y){
          int[][] a = new int[100][100];
   BufferedReader br = null;
   File f = new File("gamestate/map_"+x/100+"_"+y/100+".txt");
   if(!f.exists()){
              try {
                  f.createNewFile();
              } catch (IOException ex) {
                  Logger.getLogger(ServerState.class.getName()).log(Level.SEVERE, null, ex);
              }
             
writeArrayToFile(map_template, "gamestate/map_"+x/100+"_"+y/100+".txt");
   }
        try {
            br = new BufferedReader(new FileReader("gamestate/map_"+x/100+"_"+y/100+".txt"));
        } catch (FileNotFoundException ex) {}
try {
   for (int i = 0; i <100; i++) {
        String[] st = br.readLine().trim().split(" ");
        for (int j = 0;j < 100; j++) {
            a[i][j] = Integer.parseInt(st[i]);
        }
        
   }
      } catch (IOException ex) {}
   Chunk c = new Chunk();
   c.tiles=a;
   c.x=x/100;
   c.y=y/100;
   return c;
    }
    public static Chunk getChunk(int x,int y){
        return map[x/100][y/100];
    }

Also Chunk class is basically a 2d 100x100 int array with coordinates.

If I would send an emty array instead I would get java.nio.BufferOverflowException on server side and ofc client still disconnects.
Please say if I forgot to add some information. Kryonet seems to be configured normally and everything else works.

my guess is the message buffer size : https://github.com/EsotericSoftware/kryonet#buffer-sizes

the map array might be just too big to flush in one piece.

I tried sending short array of 2x2. Still same error on the same packet sent

We need information (code) on how you set up your serializers and registration and how the server gets set up.

Do you initialize all sub-arrays? are there any null/unset values somewhere in the array?

Try passing an appropriate serializer when registering the classes.
Do you even register the int[][] to your Kryo instance?

Have a look at the default serializers: https://github.com/EsotericSoftware/kryo#default-serializers

You can write your own serializer if none of the default ones fit, it’s actually quite doable, just have a look at the kryo(net) source code.

Here is the server configuration. Although I don’t know if I need custom serializers. My current classes get serialized well. So int array can’t be serialised by default serializers?

server = new Server();
        
        Kryo kryo = server.getKryo();
    kryo.register(LoginRequest.class);
    kryo.register(RegisterRequest.class);
    
    kryo.register(LoginResponse.class);
    kryo.register(RegResponse.class);
    kryo.register(Location.class);
    
    kryo.register(PopupMessage.class);
    kryo.register(LocalData.class);
    kryo.register(int[].class);
    kryo.register(int[][].class);
    new Thread(server).start();
        try {
            server.bind(8215);
        } catch (IOException ex) {
           
        }code]

There is an int[] serializer in the DefaultArraySerializers class, i don’t think kryo(net) handles int[][] by default.

DefaultArraySerializers.java#L66

If you do not want to write your own serializer:
Just collapse the 2d array into a 1d array and send that, then unpack on the other side like so:

{lengthRows, lengthColumns, row0, row1, row2, ... , rowN}

If this is the problem I will try to make a custom serializer. Thanks for the help :slight_smile:

Ok sorry if I am replying too late, was busy for the past few days, but kryonet handles int[][] by default. So the problem isn’t serialization. I would appreciate if someone knows anything about why the client might be disconnecting. :slight_smile:

Do you register the same classes in the same order on both client and server? This is important because the ids of the registered classes have to match.
Also what about running it with Log level debug, it might tell you the exact error.

Also:

100 x 100 x 4 byte = 40000 byte

This send-call could stall your connection for a second if it even gets past the buffer.
Do you send multiple chunks? if yes: this could overflow the buffer.

keep in mind that the buffer sizes both need to be at least as big as the largest object (40000 + some extra)!
The object buffer should be as close to the max object size as possible and the send buffer should be big enough for multiple messages of that size.

Yeah I noticed the connection is getting stalled for about 5 seconds, but I set the timeout to be a lot bigger then the average time to send the map. Also I didn’t send chunks, I only send the 40x40 map where player is. That was probably a bad idea now when I think about it, so thanks, will rework it now :slight_smile: (this might even fix my problem). Classes are registered in the same order on both server and client. Not sure about the buffer size but whole array is sent without flaws (besides client disconnecting after that).

Good luck!