Results 1 to 10 of 10
  1. #1
    eggmanpete is offline Member
    Join Date
    Feb 2009
    Posts
    17
    Rep Power
    0

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

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    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?

  3. #3
    eggmanpete is offline Member
    Join Date
    Feb 2009
    Posts
    17
    Rep Power
    0

    Default

    I'm completely new to serialization.
    Is it the ByteArrayOutputStream or the ObjectOutputStream that does the serialization?

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    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.

  5. #5
    eggmanpete is offline Member
    Join Date
    Feb 2009
    Posts
    17
    Rep Power
    0

    Default

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

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Just curious, why are you using UDP datagrams to send the data and not the more reliable (though slower) TCP protocol?

  7. #7
    eggmanpete is offline Member
    Join Date
    Feb 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Just curious, why are you using UDP datagrams to send the data and not the more reliable (though slower) TCP protocol?
    I'm working on a project which requires that I use both protocols :)

    Right the new problem is with the following code...:

    Java Code:
    System.out.println((ArrayList) ois.readObject());
    prints the ArrayList successfully, but when I do:

    Java Code:
    ArrayList board = new ArrayList();
    board = (ArrayList) ois.readObject();
    It kicks up error:
    Java 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)
    Any idea? Cheers

  8. #8
    eggmanpete is offline Member
    Join Date
    Feb 2009
    Posts
    17
    Rep Power
    0

    Default

    I figured it out.

    Java Code:
    ois.readObject();
    seems to empty the ois and as I was System.outing it, i couldn't add it to the array.

    Cheers for the help guys :)

  9. #9
    lathapon is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default

    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

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Receive uploaded image / video (byte array) from j2me on jsp.HOW???
    By angelicsign in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-29-2010, 06:21 PM
  2. Printing Byte Array
    By suchismitasuchi in forum New To Java
    Replies: 3
    Last Post: 01-19-2009, 10:58 AM
  3. Byte Array
    By sandor in forum New To Java
    Replies: 12
    Last Post: 01-15-2009, 03:31 AM
  4. Replies: 1
    Last Post: 07-04-2008, 05:39 PM
  5. Reading/Writing a File using byte array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 10:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •