?
Solved

StreamCorruptedException in new ObjectInputStream()

Posted on 2002-03-04
9
Medium Priority
?
777 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

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 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

752 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