Solved

StreamCorruptedException in new ObjectInputStream()

Posted on 2002-03-04
9
732 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Performance issue while iterating with streams 5 39
countHi challenge 25 84
changeXy challenge 13 57
Systems talking to each other 5 109
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

758 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

19 Experts available now in Live!

Get 1:1 Help Now