Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6700835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:51:43+00:00 2026-05-26T06:51:43+00:00

My question is that I have an object class( packet) which contains another object

  • 0

My question is that I have an object class( packet) which contains another object (msg) in it. I make both classes implements Serializable. But when I run my program I see this error “java.io.StreamCorruptedException: invalid type code: 00” some part of my code :

outStrm=new ObjectOutputStream(mySocket.getOutputStream());
inStrm=new ObjectInputStream(mySocket.getInputStream());
outStrm.writeObject(new packet());
outStrm.flush();

and on the server side I read like this

Socket client = listen_socket.accept();
in=new ObjectInputStream(client.getInputStream());
out=new ObjectOutputStream(client.getOutputStream());
p=(packet) in.readObject();

I send many packets and the weird part is that when I debug it(step by step) I wont see the error but when I run it I have this error! Please help me.

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

class ServerThre
    extends Thread
{
    Socket client;
    ObjectInputStream in;
    ObjectOutputStream out;

    public ServerThre(Socket client_socket)
    {
        client = client_socket;
        try
        {
            in = new ObjectInputStream(client.getInputStream());
            out = new ObjectOutputStream(client.getOutputStream());
        }
        catch (IOException e)
        {
            try
            {
                client.close();
            }
            catch (IOException e2)
            {
            }
            System.err.println("Exception while getting input streams: " + e);
            return;
        }
        this.start();
    }

    private packet getPacket() throws IOException
    {
        packet p;
        try
        {
            p = (packet)in.readObject();
            if (p != null)
            {
                return p;
            }
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void run()
    {
        while (!client.isClosed())
        {

            packet p = null;
            try
            {
                p = getPacket();
            }
            catch (IOException e1)
            {
                e1.printStackTrace();
            }

            if (p == null)
            {
                System.out.print("Connection closed by server, exiting");
                return;
            }

        }// end of while
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:51:43+00:00Added an answer on May 26, 2026 at 6:51 am

    There are several oddities in this code.

    1. The test for p != null in getPacket() is completely pointless.

    2. The later test that equates p == null with the connection being closed is incorrect. You would get an EOFException under that circumstance, not a null.

    3. Socket.isClosed() only tells you whether you have closed the socket.

    ‘Invalid type code’ usually arises when you use a single ObjectInputStream for reading and multiple ObjectOutputStreams for writing, or the other way around. Use one of each for the life of the socket at both ends.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

An interesting question arose today. Let's say I have a .NET object that implements
I have my own php data object class that wraps the mysqli library. It
I have object that is serialized into db. The question is where to keep
Main Question: I have a reference type (object/class) where I would like to specify
Assume that we have 3 classes: Place, Contact, PhoneNumber classes. Under the Place class,
I have a .NET class library that spins up a secondary process which is
I have a problem with serialisation. The class in question represents a network packet
This is a noob question. If I have a ViewController and inside that class
I have a question with fluent interfaces. We have some objects that are used
Ok, NHibernate question here. I have two objects that I would like to map

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.