Solved

StreamCorruptedException in new ObjectInputStream()

Posted on 2002-03-04
9
744 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now