Solved

StreamCorruptedException in new ObjectInputStream()

Posted on 2002-03-04
9
769 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 101
ejb wildfly example 2 103
JDBC performance 8 32
Java - accessing a parent class member when another class inherits from the parent class 9 52
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…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses

740 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