• Post Reply Bookmark Topic Watch Topic
  • New Topic

StreamCorrupted Exception

 
Kalyan Kumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to continuously send data from one VM to another VM using object serialization.
I am getting StreamCorrupted exception and not able to receive complete data that I am sending from one end.
The following is the sending Part code snippet:
The sender part is like multiple threads tring to write the data on to the Socket OutPutStream.

Following is the receiving part code snippet:

[ February 25, 2003: Message edited by: Michael Ernest ]
 
raimondas zemaitis
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You send array of bytes (just a bunch of bytes) and try to read it as an object. Suppose this won't work.
Don't write to ByteArrayOutputStream, write directly to OutputStream made from Socket, i.e.
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(obj);
Hope this should work.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why the reset before the flush? That is bound to cause problems. I don't think you should be using reset() at all!

There is nothing wrong with writing the object to a byte array first, but the only reason I can think to do it is so you can see how big it is.
Bill
 
Kalyan Kumar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I removed reset and tried teh same but still I am not able to receive all the objects.
Here I am using sender a thread that continuously sends teh data and receiver will be reading continuously and printing the data. Since I am using threads is it a problem since the readObject might not be reading as fast as writeObject.
Can we send data continuously from one end i.e., more than one thread sending data and at receiving end should be receving all teh data. Is it feasible soln.. If you have any readymade framework please let me know.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably should not be using ObjectOutputStreams then - they do all kinds of stuff behind your back.
For maximum speed I suggest you use one Thread devoted to sending blocks of data as byte[] on the sending side and one Thread devoted to reading blocks as byte[] on the receiving side. The receiving Thread should stick the received block somewhere where another process can get them.
When I have done this I have the sending Thread send a single int = the size of the byte[] to follow, that way the receiving Thread knows when it has a complete datablock. The byte[] could of course be a serialized Object, like in your code, but you don't want to use the ObjectOuputStream for your continuous flow.
Look at the DataOutputStream and DataInputStream classes in java.io.
Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!