Solved

StreamCorruptedException in new ObjectInputStream()

Posted on 2002-03-04
9
774 Views
Last Modified: 2008-09-24
In the followiing code segment, whenever the the program try to establish ObjectInputStream in, it end with exception:

java.io.StreamCorruptedException: InputStream does not contain a serialized object

If I change the initial sequence of in and out, out can always be initialled successly.  But initial of in always with this exception.

Please tell me what is wrong.

----------------------------------------

ObjectInputStream in = null;  
ObjectOutputStream out = null;

try {
    // client is an initialled socket
    in = new ObjectInputStream(client.getInputStream());
} catch (IOException e) {
    System.out.println("in failed");
    System.out.println(e);
    System.exit(-1);
} // end of try/catch

System.out.println("in done  "+in.toString());

try {
    out = new ObjectOutputStream(client.getOutputStream());
} catch (IOException e) {
    System.out.println("BLAgent -  out failed");
    System.out.println(e);
    System.exit(-1);
} // end of try/catch
0
Comment
Question by:icefire
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 6839329
1. post the WHOLE stacktrace

2. what's happening 'on the other side' of the socket ?
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6839384
Hi,

What is on another end of socket (client)? This code works for me:

package ee.net;

import java.net.*;
import java.io.*;

public class Sockets{
    public static void main(String[] args) throws Exception {
        Sockets instance = new Sockets();
        if( args.length == 1 && "--client".equals(args[0]) ){
            instance.runClient();
        }
        else if( (args.length == 1 && "--server".equals(args[0])) || args.length == 0 ){
            instance.runServer();
        }
        else{
            System.out.println("Unexpected command line");
        }
    }

    private void runClient() throws Exception {
        Socket socket = new Socket("localhost",9876);
        ObjectOutputStream os = new ObjectOutputStream(socket.getOutputStream());
        os.writeObject("What's wrong?");
        os.flush();

        ObjectInputStream is = new ObjectInputStream(socket.getInputStream());
        Object input = is.readObject();
        System.out.println("Got responce, class: "+input.getClass().getName()+", value: "+input);
        os.close();
        is.close();
        socket.close();
    }

    private void runServer() throws Exception {
        ServerSocket server = new ServerSocket(9876);
        Socket interaction = server.accept();
        ObjectInputStream is = new ObjectInputStream(interaction.getInputStream());
        Object input = is.readObject();

        System.out.println("Got input, class: "+input.getClass().getName()+", value: "+input);
        ObjectOutputStream os = new ObjectOutputStream(interaction.getOutputStream());
        os.writeObject("Everything OK");
        os.close();
        is.close();
        interaction.close();
        server.close();
    }
}

Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
0
 

Author Comment

by:icefire
ID: 6839403
hi, heyhey:

What do you mean the WHOLE stacktrace?

The other side of the socket is an applet.  The applet and server, which contain above code, are on same PC.  What I want to do is open the applet from Internet, and communicate with the server.  

Is there any basic principles I am missing?  At the time the server initial in and out, should the applet do some corresponding operation?
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:icefire
ID: 6839483
hi, heyhey:

What do you mean the WHOLE stacktrace?

The other side of the socket is an applet.  The applet and server, which contain above code, are on same PC.  What I want to do is open the applet from Internet, and communicate with the server.  

Is there any basic principles I am missing?  At the time the server initial in and out, should the applet do some corresponding operation?
0
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6839610
If you are opening the socket from an applet, you are probably opening an http connection.  The http headers are confusing your objectinputstream.  You have to read the headers - you may throw them away if you want, but create your objectinputstraem after you read to the first blank line in the inputstream.
0
 

Author Comment

by:icefire
ID: 6839681
m_onkey_boy :

How to read the header?  
0
 
LVL 4

Accepted Solution

by:
m_onkey_boy earned 100 total points
ID: 6839995
try {
   // client is an initialled socket
InputStream is = client.getInputStream();
int c;

while ((c = is.read()) != -1) {
  // c will be the next character in the header.  You
  // may handle as needed.  When you come accross two
  // '\n' characters in a rowm you have reached the end
  // of the header, so call break;
}
// Now you are past the header, so instantiate your ObjectInputStream.
//  If you want to buffer your stream, here is a good place to do it.
   in = new ObjectInputStream(is);
} catch (IOException e) {
   
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6841598
Hi,

Could you please show both sides of your code (client and server), including socket open/creation code?
heyhey_ wants to see output of Exception.printStackTrace()

Igor
 
0
 

Author Comment

by:icefire
ID: 6841955
In this program, I tried to reuse some old code.  This makes my code unnecessarily long.  That is why I did not post it here.  

I checked Exception.printStackTrace(), the question part is
 in = new ObjectInputStream(client.getInputStream());  
Not much help.

I solved the problem with m_onkey_boy's method.

Thank you all for your kind help.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question