Packets serialize + deserialize

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

     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?

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.

thx for answer ;D
No I dont need the string anymore :slight_smile:
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:

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

public class serialize implements Serializable{
	private static final long serialVersionUID = 1L;

best regards Phibedy

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

thx :slight_smile:
Do you have any idea how to fix my other problem?

I fixxed it :slight_smile:
Thx for help, I spent a lot of time in saving Objects in a txt-file -> Failed ::slight_smile:

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).

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 ;D
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

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. :point:

thx for fast reply :slight_smile:
Now I have to write my own “tcp”-methods, I think it will be fun ;D

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. :slight_smile:
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!

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.

Sry I maybe asked the in wrong way, I already got code to send packets and put them together afterwards.

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.

thx for help, works fine now ;D