Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Packets serialize + deserialize  (Read 3536 times)
0 Members and 1 Guest are viewing this topic.
Offline Phibedy

Senior Member


Medals: 8



« Posted 2012-07-16 15:59:07 »

Hi, I would like to sent and Object2 via UDP, (object2 could be everything)
therefore I create a new Object (String,Object2). The String tells the deserializer what object2 is.
Do I really need the String or is it possible to get the type of the packet out of the packet?
best regards Phibedy
Offline Damocles
« Reply #1 - Posted 2012-07-16 16:52:06 »

         FileInputStream fileIn =
                       new FileInputStream("employee.ser");
         ObjectInputStream in = new ObjectInputStream(fileIn);

         Object o=in.readObject();
         System.out.println(o.getClass());
        
         if(o.getClass() == Employee.class)  em = (Employee)o;
         if(o.getClass() == Dog.class)  dg = (Dog)o;
         if(o.getClass() == Bill.class)  bl = (Bill)o;

-> with .getClass you can extract the class of the serialized Object, and then cast it

Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #2 - Posted 2012-07-16 18:00:58 »

        FileInputStream fileIn =
                       new FileInputStream("employee.ser");
         ObjectInputStream in = new ObjectInputStream(fileIn);

         Object o=in.readObject();
         System.out.println(o.getClass());
        
         if(o.getClass() == Employee.class)  em = (Employee)o;
         if(o.getClass() == Dog.class)  dg = (Dog)o;
         if(o.getClass() == Bill.class)  bl = (Bill)o;

-> with .getClass you can extract the class of the serialized Object, and then cast it

You can use the instanceof keyword as well. I would use something that takes less memory than a string. How about assigning ID's and using a byte?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava
« Reply #3 - Posted 2012-07-16 18:07:38 »

A Byte message identified you say? Old school. Using objects is less of a hassle and cleans up your protocol a lot imo. No need to keep track of id's. The standard java serialization isn't the best one out there, you should definitely check KryoNet out: http://code.google.com/p/kryonet/

It's a simple API that uses java NIO and an enhanced serialization library.

Offline Phibedy

Senior Member


Medals: 8



« Reply #4 - Posted 2012-07-16 18:11:35 »

thx for answer  Grin
No I dont need the string anymore Smiley
The object includes 3 ArrayLists with objects + int[200][200] + some other stuff.
Is it a problem to send it via udp?

edit:
Another problem:
       
1  
2  
3  
4  
5  
private String worldName;
   private String worldPartName;
   private int[][] worldPart;
   private int startTileX;
   private ArrayList<ObjectEntity> objectRenderList;


"java.io.NotSerializableException: de.MineForce.Loading.MapObject
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: de.MineForce.Loading.MapObject"

I don´t know why, because there is no thread in the object
and
1  
2  
public class serialize implements Serializable{
   private static final long serialVersionUID = 1L;


best regards Phibedy
Offline Damocles
« Reply #5 - Posted 2012-07-16 18:41:37 »

true, 
 if(o instanceof Employee)  e = (Employee)o;

also works (just wanted to show that the class-definition can be read out from the serialized object)

Well UDP is not guaranteed to arrive.

So locally on your computer or Lan everything will work fine.
But in a longer online session you might experience lost and unsorted packets using UDP

Either its not important (streamed media) or you build your own validation code (complex)

TCP is then still an alternative

Offline Phibedy

Senior Member


Medals: 8



« Reply #6 - Posted 2012-07-16 18:58:40 »

thx Smiley
Do you have any idea how to fix my other problem?
Offline Phibedy

Senior Member


Medals: 8



« Reply #7 - Posted 2012-07-16 22:20:47 »

I fixxed it  Smiley
Thx for help, I spent a lot of time in saving Objects in a txt-file -> Failed  Roll Eyes
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #8 - Posted 2012-07-18 20:18:58 »

A Byte message identified you say? Old school. Using objects is less of a hassle and cleans up your protocol a lot imo. No need to keep track of id's. The standard java serialization isn't the best one out there, you should definitely check KryoNet out: http://code.google.com/p/kryonet/

It's a simple API that uses java NIO and an enhanced serialization library.

I meant a byte identifier, and then the payload can be whateve you'd like. Having a uniform way of identifying packets is handy, such as assuming that the first byte tells what type of packet is on the line, and thereby what the payload contains.

Ofcourse, a packet is always just an array of bytes but we can convert to other types once we know what the packet contains.

I really like KryoNet, as it does not only what I just explained, but it also has a very nice way of ordering the payload (serializing objects).

Offline Phibedy

Senior Member


Medals: 8



« Reply #9 - Posted 2012-07-18 21:27:41 »

KryoNet seems to have great features, but I am just a beginner, that´s why I wanna do as much as possible on my own to learn sth.
I got another problem  Grin
The object I wanna send is 95 kb
-> java.net.SocketException: The message is larger than the maximum supported by the underlying transport: Datagram send failed
I only send it once, all my other pakets are small enough, is it possible to sent it without spliting it?
best regards Phibedy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 798
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2012-07-18 21:29:31 »

...
The object I wanna send is 95 kb
...
is it possible to sent it without spliting it?
...
No. And once you split it, you have to deal with unguaranteed & unordered arrival.

You should really switch to TCP if you want to get anything done. Pointing

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Phibedy

Senior Member


Medals: 8



« Reply #11 - Posted 2012-07-18 21:49:35 »

thx for fast reply  Smiley
Now I have to write my own "tcp"-methods, I think it will be fun  Grin
Offline Phibedy

Senior Member


Medals: 8



« Reply #12 - Posted 2012-07-19 09:55:51 »

I got another question,
Sender: At the moment I create a new byte[] out of the (1) byte[] I wanna send and a byte[] that tells me, what the other (2) byte[] is about.

Receiver: gets the byte[] out of the paket, splits it with Arrays.copyOfRange([..]), analyses the (2). After that I create a new byte[] out of all the other byte[]s.
Is there a more efficient way of doing that?

best regs. Phibedy
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #13 - Posted 2012-07-19 16:03:05 »

I got another question,
Sender: At the moment I create a new byte[] out of the (1) byte[] I wanna send and a byte[] that tells me, what the other (2) byte[] is about.

Receiver: gets the byte[] out of the paket, splits it with Arrays.copyOfRange([..]), analyses the (2). After that I create a new byte[] out of all the other byte[]s.
Is there a more efficient way of doing that?

best regs. Phibedy

Holy crap, I could not follow that. I will write what you should be doing with the bytes, and you compare yourself.  Smiley
Every packet you will send will contain a single array of bytes (byte[]).
You can make the assumption that the first byte will always tell what is going to be in the payload (the other bytes).

Consider this packet:
byte[FF, 1F, 5A, C9, 11] (hex codes)

The first byte (byte[0]) will tell us what the packet contains. You can hold that towards some scheme you'll define in code.
Let's assume that the FF-byte represents a packet that only contains a single int.

From here you can assume that the payload is the next four bytes (you might want to check the packet length, just because the client shouldn't be trusted), and that those four bytes together will form an int.

Cheers!

Offline jonjava
« Reply #14 - Posted 2012-07-19 17:48:11 »

I got another question,
Sender: At the moment I create a new byte[] out of the (1) byte[] I wanna send and a byte[] that tells me, what the other (2) byte[] is about.

Receiver: gets the byte[] out of the paket, splits it with Arrays.copyOfRange([..]), analyses the (2). After that I create a new byte[] out of all the other byte[]s.
Is there a more efficient way of doing that?

best regs. Phibedy

Hi I think you might find this post useful: http://www.java-gaming.org/topics/game-server-general-questions/26527/msg/233042/view.html#msg233042

Remember, whatever you send anywhere to anyone from any place is always in bytes. So anything you send will eventually be translated to bytes (1010100001011) and the one who receives that message is left to figure out what the hell those one's and zero's are supposed to represent. It could be a String, an image, music.. it could be ANYTHING. The receiver will have to ASSUME what it is, he has 0 chance of figuring it out. That's why we have "protocols". A protocol is basically simply an instruction set for two people (or computers) who want to communicate with each other.

Offline Phibedy

Senior Member


Medals: 8



« Reply #15 - Posted 2012-07-19 21:15:56 »

Sry I maybe asked the in wrong way, I already got code to send packets and put them together afterwards.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public static ArrayList<byte[]> splitPacket(byte[] bytes){
      ArrayList<byte[]> chunkList = new ArrayList<byte[]>();
      int size = 40000;
      int timesRunned;
      byte[] chunkSendNumber = ServerData.getSendNumber();
      for(timesRunned = 0; bytes.length - timesRunned*size > size; timesRunned++ ){
         byte[] tempchunk = Arrays.copyOfRange(bytes,size*timesRunned,size*(timesRunned+1));
         byte chunkPart = (byte) timesRunned;
         byte[] chunk = new byte[40004];
         chunk[0] = 1;
         chunk[1] = chunkSendNumber[0];
         chunk[2] = chunkSendNumber[1];
         chunk[3] = chunkPart;
         for(int i = 0; i < tempchunk.length  ;i++){
            chunk[4+i] = tempchunk[i];
         }
         
         chunkList.add(chunk);

The client analyses the paket and put the right parts together,
I would like to know, if there is a way, to put the first 4 bytes in the same packet and analyses them, without putting them in the byte[] of the object-part, because the client has to seperate it afterwards, so it would save time.
best regs Phibedy
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #16 - Posted 2012-07-20 21:28:41 »

Sry I maybe asked the in wrong way, I already got code to send packets and put them together afterwards.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public static ArrayList<byte[]> splitPacket(byte[] bytes){
      ArrayList<byte[]> chunkList = new ArrayList<byte[]>();
      int size = 40000;
      int timesRunned;
      byte[] chunkSendNumber = ServerData.getSendNumber();
      for(timesRunned = 0; bytes.length - timesRunned*size > size; timesRunned++ ){
         byte[] tempchunk = Arrays.copyOfRange(bytes,size*timesRunned,size*(timesRunned+1));
         byte chunkPart = (byte) timesRunned;
         byte[] chunk = new byte[40004];
         chunk[0] = 1;
         chunk[1] = chunkSendNumber[0];
         chunk[2] = chunkSendNumber[1];
         chunk[3] = chunkPart;
         for(int i = 0; i < tempchunk.length  ;i++){
            chunk[4+i] = tempchunk[i];
         }
         
         chunkList.add(chunk);

The client analyses the paket and put the right parts together,
I would like to know, if there is a way, to put the first 4 bytes in the same packet and analyses them, without putting them in the byte[] of the object-part, because the client has to seperate it afterwards, so it would save time.
best regs Phibedy

At the lowest level: No.

I dont know how the networking youre working with is abstracted, and how much. You might be able to because someone else did it (like Kryonet), but at the lowest level it will always just be bytes that youre sending.

Offline Phibedy

Senior Member


Medals: 8



« Reply #17 - Posted 2012-07-24 10:54:43 »

thx for help, works fine now  Grin
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (28 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (73 views)
2014-09-10 06:39:09

Tekkerue (37 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (44 views)
2014-09-07 01:13:42

Longarmx (27 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (34 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!