StreamCorruptedException: invalid type code: AC
Hello!
I'm just writing a small application which is supposed to send serialized objects via network. The first object a client sends to the server is correctly answered by the server (sends an answer object). However, every further object the client sends causes a StreamCorruptionError on the server side. This problem occurs always when server and client are on localhost. If they are on two different machines it seems to work. Any ideas why is that?
I guess there is some conceptual mistake in my code. How to handle the ObjectOutputStreams and ObjectInputStreams correctly.
Here is the main client code:
publicclass Clientimplements Runnable, Base{
finalstaticint port = 1234;
String host="";
int timeOut = 0;
private Socket socket;
privateboolean close=false, connected;
protected Vector<Listener> listeners =new Vector<Listener>();
private Object NewObject =null;
public Client(String host){
this.host=host;
}
publicvoid run(){
close=false;
while(!close){
ObjectInputStream ois=null;
try{
if (socket==null || !socket.isConnected()){
socket =new Socket(host, port);
socket.setSoTimeout(timeOut);
connected=true;
}
ois =new ObjectInputStream(new BufferedInputStream( socket.getInputStream()));
}catch (Exception e){
e.printStackTrace();
close=true;
}
try{
NewObject = ois.readObject();
localSend(NewObject);
}catch (Exception e){
e.printStackTrace();
close=true;
}
}//while
//Connection closed -> clean up a little
try{
if (socket.isConnected())
socket.close();
socket=null;
}catch (IOException ioe){}
finally{
connected=false;
}
}
publicvoid closeConnection(){
close=true;
try{
if (socket!=null)
socket.close();
}catch (IOException ioe){}
}
publicvoid deleteListener(Listener listener){
if (listener!=null && this.listeners.contains(listener))
this.listeners.remove(listener);
}
publicvoid newListener(Listener listener){
if (listener!=null && !this.listeners.contains(listener))
this.listeners.add(listener);
}
publicsynchronizedvoid send(Object o){
ObjectOutputStream oos =null;
//debug
if (socket==null){
return;
}
try{
oos =new ObjectOutputStream(socket.getOutputStream());
}catch (Exception e){
}
try{
oos.writeObject(o);
oos.flush();
oos.reset();
}catch(Exception e){
}
}
/**
* Sends a received Object to the registered listeners
* listeners must implement the Listener interface
* @param o Object to send
*/
publicvoid localSend(Object o){
for (Iterator iter = listeners.iterator(); iter.hasNext();){
Listener element = (Listener) iter.next();
if (element!=null){
element.received(o);
}
}
}
publicvoid startConnection(){
try{
}catch (Exception e){e.printStackTrace();}
new Thread(this).start();
}
publicboolean isConnected(){
return connected;
}
}
This is the server:
publicclass Serverextends Clientimplements Runnable{
privatefinalstaticint maxClients=8;
private ServerSocket serverSocket=null;
private Vector<Socket> clientSockets =new Vector<Socket>();
private Vector<InetAddress> clientPorts =new Vector<InetAddress>();
private GameServer gs;
private ObjectInputStream ois;
privatestatic HashMap<Socket, ObjectOutputStream> ooss =new HashMap<Socket, ObjectOutputStream>();
public Server(){
gs=new GameServer(this);
this.newListener(gs);//to ensure the GameServer receives the objects sent by the clients
}
publicvoid run(){
try{
serverSocket =new ServerSocket(port);
Socket socket=null;
while (!close){
try{
socket = serverSocket.accept();
socket.setSoTimeout(timeOut);
if (!clientSockets.contains(socket) &&
!clientPorts.contains(socket.getInetAddress())){
clientSockets.add(socket);
clientPorts.add(socket.getInetAddress());
ooss.put(socket,new ObjectOutputStream(socket.getOutputStream())) ;
ServerThread st =new ServerThread(socket,new ObjectInputStream(new BufferedInputStream( socket.getInputStream())),this);
new Thread(st).start();
}
}catch (SocketTimeoutException se){
System.out.println(se.getMessage());
}catch (IOException ioe){
System.out.println(ioe.getMessage());
}
}
}catch (BindException be){//outer catch
System.out.println(be.getMessage());
}catch (Exception e){
if (e!=null){
System.out.println(e.getMessage());
}
}
}
/**
* Sends an object to the specified socket
* @param o the object to be sent
* @param socket of the client which is addressed
*/
publicsynchronizedvoid send(Object o,Socket socket){
ObjectOutputStream oos =null;
if (socket!=null && (socketinstanceof Socket) && socket.isConnected()){
try{
oos = ooss.get(socket);
}catch (Exception e){
}
try{
oos.writeObject(o);
oos.flush();
oos.reset();
}catch(Exception e){
}
}
}
publicvoid closeConnection(){
close=true;
try{
if (serverSocket!=null)
serverSocket.close();
}catch (IOException ioe){}
}
/**
* Sends a received Object to the registered listeners
* listeners must implement the Listener interface
* sends the receiving socket as well
* @param o Object to send
*/
publicvoid localSend(Object o, Socket socket){
if (o!=null && oinstanceof String){
Log.log((String) o);
return;
}
for (Iterator iter = listeners.iterator(); iter.hasNext();){
Listener element = (Listener) iter.next();
if (element!=null){
element.received(o, socket);
}
}
}
publicvoid removeClient(Socket socket){
if (socket!=null)
try{
ooss.remove(socket);
clientSockets.remove(socket);
clientPorts.remove(socket.getInetAddress());
GameServer.removePlayer(socket);
}catch (Exception e){
System.out.println(e.getMessage()+"\n");
}
}
}
This is the ServerThread (for listening):
publicclass ServerThreadextends Thread{
private Server server;
private Socket socket;
privateint numRetries=3;
private ObjectInputStream ois;
public ServerThread(Socket socket, ObjectInputStream ois, Server server){
this.socket=socket;
this.ois=ois;
this.server=server;
}
publicvoid run(){
boolean close=false;
int tries=0;
Object NewObject;
while(!close){
//read object from inputstream
try{
NewObject = ois.readObject();
server.localSend(NewObject, socket);
}catch (StreamCorruptedException sce){
System.out.println("StreamCorruptedException :-(.\n");
System.out.println(sce.getMessage()+"\n"+sce.getStackTrace()+"\n");
sce.printStackTrace();
close=true;
}catch (ClassNotFoundException ce){
System.out.println(ce.getMessage()+"\n");
close=true;
}catch (IOException ioe){
System.out.println(ioe.getMessage()+"\n");
ioe.printStackTrace();
close=true;
}catch (Exception e){
System.out.println(e.getMessage()+"\n"+e.toString()+"\n");
System.out.println("Socket: "+socket.toString()+"\n");
if (tries<=numRetries){
tries++;
continue;
}
close=true;
}
}// while
//remove socket from list of clients
server.removeClient(socket);
}
}
Thanks for your help in advance.
Yours,
Peter

