Results 1 to 10 of 10
- 02-14-2009, 06:33 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 17
- Rep Power
- 0
Sending an ArrayList over UDP, as a byte array.
Hi guys, I'm not exactly sure what I am doing, so I'll need a bit of help with this one.
I have a server sending a packet with an ArrayList over a UDP socket by converting it to a byte [].
then the client reads the packet and converts the byte [] back to an ArrayList.
The error I'm getting is this...
Java Code:14-Feb-2009 17:34:35 UI.input$1 sendUpdate SEVERE: null java.io.StreamCorruptedException: invalid stream header: EFBFBDEF at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783) at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280) at networkUDP.udpclient.udpsend(udpclient.java:48) at UI.input$1.sendUpdate(input.java:30)
CLIENT:
Java Code://prepare for intercepting a reply from server // this reply will contain the updated game board DatagramPacket receivePacket = new DatagramPacket(receiveByte, receiveByte.length); udpSocket.receive(receivePacket); //extract data from received packet and print it String incomingWord = new String(receivePacket.getData()); System.out.println(incomingWord); udpSocket.close(); byte [] incomingBytes = incomingWord.getBytes(); // de-serialize //ByteArrayOutputStream abcd = new ByteArrayOutputStream(); ByteArrayInputStream bais = new ByteArrayInputStream(incomingBytes); ObjectInputStream ois = new ObjectInputStream(bais); //TODO: MAKE INTO incomingWord // output board ArrayList board = (ArrayList) ois.readObject(); ois.close(); int x =0; while (x != board.size()) { System.out.println(board.get(x)); x++; } x=0;
SERVER:
Java Code:finishBoard = board.updateBoard(col, row); //TODO: serialize the array list here and sent it // then read the serialsed object back at the client ByteArrayOutputStream fos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(finishBoard); oos.close(); // end serialization sendByte = fos.toByteArray(); DatagramPacket sendPacket = new DatagramPacket( sendByte, sendByte.length, IP, port); serverSocket.send(sendPacket);
Thanks for any help :confused:Last edited by eggmanpete; 02-14-2009 at 07:48 PM.
- 02-15-2009, 03:39 AM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
The error you're getting indicates the stream is not a serialized object. Are you sure your server is the only thing using that group address and port? If so, is it possible your ArrayList is being modified by another thread while it's being serialized?
- 02-15-2009, 03:42 AM #3
Member
- Join Date
- Feb 2009
- Posts
- 17
- Rep Power
- 0
I'm completely new to serialization.
Is it the ByteArrayOutputStream or the ObjectOutputStream that does the serialization?
- 02-15-2009, 03:59 AM #4
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
The ObjectOutputStream does the serialization. The ByteArrayOutputStream is just an intermediary that converts an output stream to a byte array.
Also, it occurs to me that whatever Objects you're putting into the ArrayList might not be serializable. Are you sure they are?
Try an experiment where you send just an empty new ArrayList, and verify that works ok (it should). Once you've convinced yourself the bug really is somewhere in your own code, you can work backwards to find it.
- 02-15-2009, 08:11 PM #5
Member
- Join Date
- Feb 2009
- Posts
- 17
- Rep Power
- 0
Right, i have somehow fixed that error. Can't remember how but I'll post my code in a bit.
Now I have a new problem.
error is:
java.io.StreamCorruptedException: invalid type code: 00
at java.io.ObjectInputStream.readObject(....
any idea?
at least I'm getting somewhere :)
-
Just curious, why are you using UDP datagrams to send the data and not the more reliable (though slower) TCP protocol?
- 02-15-2009, 10:37 PM #7
Member
- Join Date
- Feb 2009
- Posts
- 17
- Rep Power
- 0
I'm working on a project which requires that I use both protocols :)
Right the new problem is with the following code...:
prints the ArrayList successfully, but when I do:Java Code:System.out.println((ArrayList) ois.readObject());
It kicks up error:Java Code:ArrayList board = new ArrayList(); board = (ArrayList) ois.readObject();
Any idea? CheersJava Code:15-Feb-2009 21:36:00 UI.input$1 sendUpdate SEVERE: null java.io.StreamCorruptedException: invalid type code: 00 at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1356)
- 02-15-2009, 11:00 PM #8
Member
- Join Date
- Feb 2009
- Posts
- 17
- Rep Power
- 0
I figured it out.
seems to empty the ois and as I was System.outing it, i couldn't add it to the array.Java Code:ois.readObject();
Cheers for the help guys :)
- 11-12-2010, 12:19 PM #9
Member
- Join Date
- Nov 2010
- Posts
- 1
- Rep Power
- 0
I am getting the same issue "java.io.StreamCorruptedException: invalid type code: 00" in my JBOSS server version 4.0.4 GA. with jdk1.5. Please let me know how to correct this. I am returning arraylist which is serialized
-
lathapon: it's probably best for you to ask your new question in its own thread and in that thread link to this one. It will increase your chances of getting help.
Similar Threads
-
Receive uploaded image / video (byte array) from j2me on jsp.HOW???
By angelicsign in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 06-29-2010, 06:21 PM -
Printing Byte Array
By suchismitasuchi in forum New To JavaReplies: 3Last Post: 01-19-2009, 10:58 AM -
Byte Array
By sandor in forum New To JavaReplies: 12Last Post: 01-15-2009, 03:31 AM -
urgent please.what do 4 repeating fields in byte array of jpeg specify?
By hemant in forum Java 2DReplies: 1Last Post: 07-04-2008, 05:39 PM -
Reading/Writing a File using byte array
By Java Tip in forum Java TipReplies: 0Last Post: 01-16-2008, 10:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks