StreamCorruptedException ?!

I'm able to do the following with no problems at all:


Socket server=..;
..

out=new DataOutputStream(server.getOutputStream());
in =new DataInputStream(server.getInputStream());
oos=new ObjectOutputStream(server.getOutputStream());


But when I try the following:


ois=new ObjectInputStream(server.getInputStream());


I get the following exception:

java.io.StreamCorruptedException: invalid stream header: 00095253
      at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
           .
           .

(on the ObjectInputStream line..)


Help? :(

Thanks
LVL 25
InteractiveMindAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
you cannot open a ObjectInputStream and DataInputStream on the same stream like that, ?(and it doesn't make a lot of sense to do it).
You can try opening the OIS first, but I'd suggest not opening a DataInputStream.
0
 
InteractiveMindAuthor Commented:
(The server-side is able to open all four streams no problem..)
0
 
CEHJConnect With a Mentor Commented:
Was the stream *written* with ObjectOutputStream?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
InteractiveMindAuthor Commented:
Sorry for the delay guys;

I now see that using a DataInputStream and an OutputInputStream is rather illogical; so I've cut it down to just an ObjectInputStream. This has stopped the exception.

However, now, although I'm able to open an ObjectOutputStream with no problem
oos=new ObjectOutputStream(server.getOutputStream());
my code just hangs when I attempt to open an ObjectInputStream
ois=new ObjectInputStream(server.getInputStream());

(I open the ois straight after the oos)

I'm stumped.
0
 
CEHJConnect With a Mentor Commented:
Is that happening *before* you try to read from that input stream?
0
 
InteractiveMindAuthor Commented:
yes
0
 
CEHJConnect With a Mentor Commented:
Is it definitely this line that causes it to hang?

>>ois=new ObjectInputStream(server.getInputStream());
0
 
InteractiveMindAuthor Commented:
I'm pretty sure it is, yea.

I'm doing the following to make sure:

oos=new ObjectOutputStream(server.getOutputStream());
System.out.println("o");
ois=new ObjectInputStream(server.getInputStream());
System.out.println("i");

It outputs o, but no i (and there's no exception or anything..)  :-\
0
 
InteractiveMindAuthor Commented:
well, i'm in the process of recoding my program.
hopefully that will solve the problem (as i'll be more cautious..); i'll get back to ya'll soon.
0
 
objectsCommented:
> my code just hangs when I attempt to open an ObjectInputStream

change the order you open things at the other end
0
 
InteractiveMindAuthor Commented:
I tried that as well :(


As it goes though, I finished recoding the program just a few mins ago--and am not getting any problems with it :-)
So I'll just put this hanging problem down to bad coding :-P  (it's a large code-and-fix app)

So, I will split the points (although most will be to objects for his solution to my initial issue).


Thanks very much both
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.