• Post Reply Bookmark Topic Watch Topic
  • New Topic

StreamCorruptedException: invalid stream header: only when running program for the second time  RSS feed

 
Christo Goosen
Greenhorn
Posts: 3
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've been searching for just about a week now and tried everything in the book.
I have a small client - server program using sockets where the client logs on to the server and retrieves data from the database.
Using ObjectOutputStream and ObjectInputStream receives the objects, etc.

The code works perfectly well and I can do multiple transactions without a problem.
As soon as I stop and re-start the client it connects successfully to the server but the first object I receive back from the server throws StreamCorruptedException: invalid stream header:
I checked many posts and examples and I know when the server submits and the client receives and the code knows when to expect the data (found many discussions around that). I traced it many times in the debugger but no luck.
The server sends a message to the client to indicate the type of data to follow so for example it will send the word "login" to tell the client that the User object is coming next.
Also tried to change the way I initialize ports and sockets as well as closing all when exiting the program.
When re-starting the server and then the client everything is fine again.
Please help


java.io.StreamCorruptedException: invalid stream header: 00000001
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:298)
at rdsclient.FRdsLogin.handleLoginDetails(FRdsLogin.java:299)
at rdsclient.Network$IncomingReader.run(Network.java:102)
at java.lang.Thread.run(Thread.java:722)


Server Code: Sending
Tell the client to expect User object


Now send the actual object...



Client Code
Following client code runs in a thread

 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only obvious thing I can see is that your method tellEveryone() sends characters and not objects! If you are trying to read the message as a serialized object then this will cause the exception.

Either use serialized objects throughout or String throughout but don't mix.

P.S. I have found that it is normally better to forget serialization and use DataInputStream and DataOutputStream for this sort of simple text communication task.
 
Christo Goosen
Greenhorn
Posts: 3
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply but I do not understand why it would work perfectly well over and over until I re-start the client. Hence I thought it could be something relating to initialization?
I can try the DataInputStream and DataoutputStream as you suggested but most of the hard labor is going to be sending objects and arrays of objects around.
The simple text is purely to tell the client what to expect next.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christo Goosen wrote:Thanks for the reply but I do not understand why it would work perfectly well over and over until I re-start the client. Hence I thought it could be something relating to initialization?

It probably is an initialization problem in that one side is sending an initial String and the other expecting a serialized object. As I said - don't mix text and serialized objects then this exception cannot happen. It sounds to me like you should just be using serialized objects.

I can try the DataInputStream and DataoutputStream as you suggested but most of the hard labor is going to be sending objects and arrays of objects around.
The simple text is purely to tell the client what to expect next.


If you are sending array and the like then for the moment stick with serialized objects.
 
Christo Goosen
Greenhorn
Posts: 3
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a million.
I changed it all to use serialized objects throughout and it is working OK now.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!