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
    }

}
MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
Sara
  • 2,308
  • 11
  • 50
  • 76
  • You need to post your code and the stack trace. The problem is in code you *aren't* showing, not in the 8 lines you posted. – Brian Roach Oct 18 '11 at 04:22
  • Is it true that I made both of my classes implements serializable?My total code is a lot. – Sara Oct 18 '11 at 04:24
  • 1
    Lets try this - read this: [SSCCE](http://sscce.org/). Create one that demonstrates the problem you are having. Come back, post that. No one can help you as it is. There's nothing wrong with the snippit you posted. – Brian Roach Oct 18 '11 at 04:30

1 Answers1

0

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.

user207421
  • 305,947
  • 44
  • 307
  • 483