How does one send a tiled map TMX + Spritesheets through TCP using Kryonet?

I want the Client to fetch the map from the Server, what is the best approach to achieve this?

Send them all separately. Include a header in the packet identifying the data the packet will contain. Create a parser for each packet, and use X parser depending on X header.

example:
Packet: tile map
Header: 0, 1

Bytes of data: 0, 1, mapData…

Then just have a parser to parse the map data into something usable for the client. The client should see the first two bytes as 0 and 1 (or whatever header) and go “oh, i should use the map parser!”

This is how you do it in most networking as far as i know, so it should apply to Kryonet as well.

Maybe a bit late but you can just send the file object to the client.

when sending server->client
file = Paths.get(“location/somefile.fileformat”).toFile();

void received() {
file.mkDir();
}

The file object is just a reference to the file’s location, nothing more.

You can send the bytes in the file and rewrite the file, but you can’t just

[quote=“SuperMario,post:3,topic:54619”]

[quote=“CopyableCougar4,post:4,topic:54619”]
The file object is just a reference to the file’s location, nothing more.

You can send the bytes in the file and rewrite the file, but you can’t just

With Kryonet you can do this. (I’m pretty sure. I’ve never tried…)

Load the tmx data into an object and send it to the client

Kryonet serializes the object. I believe you can simply cast it:


public void received (Connection connection, Object object) {
    if (object instanceof SomeRequest) {
        SomeRequest request = (SomeRequest)object;
        System.out.println(request.text);

        SomeResponse response = new SomeResponse();
        response.text = "Thanks";
        connection.sendTCP(response);
    }
}

Yes, you can cast it but you need to register the class for deserialization before hand. The Client/Server objects have a getKryo() method where you can register your classes. Make sure to register them in the same order. If they are out of order it will completely differ when deserializing.

Kryonet does serialization of each field, and no field in File objects directly reference the contents. Hence, you cannot just send a file object over the network and expect to load the contents on the other side.

You can send the tiledmap object, but not the File object.

       SendFile file = new SendFile();
		file.file = Paths.get("./assets/maps/map.tmx").toFile();
		connection.sendTCP(file);

Is what I use. Works fine for me.

I think I mistyped what I wanted to say. Object probably isn’t the correct word.

Hmm… That’s interesting.

I would’ve done it like this:


        //server
        File file = new File("file.dat");
        try (InputStream in = new FileInputStream(file)) {
            byte[] data = new byte[(int) file.length()];
            in.read(data);            
            //send data[]            
        } catch (Exception ex) {
        }
        
        //client
        byte[] data = (byte[]) object;
        try (OutputStream out = new FileOutputStream("name of file...")) {
            out.write(data);
            out.flush();
        } catch (Exception ex) {
        }

That’s how I done it first. But then I said to myself why convert it into bytes if there’s Kryo.