1

I have got a question....

Here we got an 2d byte array :

byte[][] duengonMap = new byte[500][500];

Because i wanna send it from the client to the server or the other way round, i need to put it into a int/long. From the server it will be send to the other connected clients and there it is gonna convert back to an 2d array. It sounds so simple ... but how do i do that ?

I tried something like that :

int[][] twoD = new int[][] { new int[] { 1, 2 },
        new int[] { 3, 4 } };

int[][] newTwoD = null; // will deserialize to this

System.out.println("Before serialization");
for (int[] arr : twoD) {
    for (int val : arr) {
        System.out.println(val);
    }
}

try {
    FileOutputStream fos = new FileOutputStream("test.dat");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(twoD);

    FileInputStream fis = new FileInputStream("test.dat");
    ObjectInputStream iis = new ObjectInputStream(fis);
    newTwoD = (int[][]) iis.readObject();

} catch (Exception e) {

}

System.out.println("After serialization");
for (int[] arr : newTwoD) {
    for (int val : arr) {
        System.out.println(val);
    }
}
}

It only is converted into an "File" maybe i did that wrong, but i have no idea how to convert it to an int or long ... Any ideas ? Or examples ?

genaray
  • 1,080
  • 1
  • 9
  • 30
  • Why do you think you need to convert to int? `byte[][]` is an object; so you can simply send it as is; using `writeObject()`. Or am I missing something here? – GhostCat Mar 10 '16 at 17:50
  • Yeah because i am using Kryonet, didnt mentioned that. And there you cant send bytes[][] or large Objects over udp or tcp, than the server will crash :/ – genaray Mar 10 '16 at 17:51
  • No one got an solution? – genaray Mar 11 '16 at 06:14

2 Answers2

0

You can send all the values One by One using a loop and read them simultaneously on the server/client and arrange them back into an array

  • Thanks for your answer :) That would work, but what if you want to share your map ? Then youll need a seed. Furthermore it causes to much traffic between server and client :/ – genaray Mar 10 '16 at 18:13
0

Basically, there are two problems to solve.

1) How to turn a byte into an int and vice versa ... which is pretty straight forward, and has been answered here probably countless times. See here fore example

2) Now, that you know how to send one byte/int ... the only thing you have to do ... is to define an order in which you want to traverse your array. Meaning: you traverse your byte array, send each byte as int; and on the other side, you read the int values, and populate your byte array using the same traversal algorithm.

Of course, the easiest solution would be nested for loops; but depending on your data; you might find other ideas helpful. For example: if your byte array contains many 0 values ... you might decide to only transfer the non-0 values (like sending a pair consisting of index+value).

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Thanks for your fast reply :) i just looked at the example... this only works on 1d byte arrays or on 2d too ? I also see, that its putting just one value in the int ... but how to put a whole 2d array into it ? O.o thats a good idea with sending only the non 0 values ... just tried it but dont work either, my map seems to be to big :( – genaray Mar 10 '16 at 18:12
  • Sorry please read my answer. You TRAVERSE your array, and then you send single bytes as int. And are you sure that you are sending too much data? From the way you are asking it seems more likely, that your implementation is **broken**, and therefore it is not working. In that sense: open up a new question; post your code; including a full description of the errors you encounter. In other words: what makes you think that you are hitting a "data volume" limit? – GhostCat Mar 10 '16 at 18:31
  • Ok now i understand it, ill give it a try, why there arent any librarys for that? :) No my implementation isnt broken, because everything works, only the part where i transfer the byte[][] is stucking ... I dont get any error messages, the server just shut down without any exception – genaray Mar 10 '16 at 18:39
  • Just to be sure: that means that you fully tested the both sides; and you also made sure that other client/server communication works? What I mean is: i **highly** doubt that a crashing server means "too much data". To the contrary, if your server crashes upon running your code, I assume your code is broken. – GhostCat Mar 10 '16 at 18:46
  • Yeah, no worries the multiplayer server worked fine for weeks, it only crashes when i try to send my byte[][] or object which includes byte[][] When you google kryonet 2d array you find much crash reports but not one simple solution... The most ended up with : "Use seeds" or transform your array into an int (seed) and transform it back. Without sending byte[][] it works fine – genaray Mar 10 '16 at 18:54
  • So tried to send parts of the 2d array within a for loop... i dont know why but theres an problem ... on the client side i can pack all my vars in to an Packet, called TilePacket. The vars are allright, but when they receive on the server side there are different values O.o and not every package is arrived... I think it would be easier to implement a seed system or wrap the god damned array, any ideas ? – genaray Mar 10 '16 at 20:04