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