Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

StreamCorruptedException: invalid type code: AC

843790May 27 2007 — edited Dec 3 2008
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:
public class Client implements Runnable, Base {
	final static int port = 1234;
	String host="";
	int timeOut = 0;
	private Socket socket;
	private boolean close=false, connected;
	protected Vector<Listener> listeners = new Vector<Listener>();
	private Object NewObject = null;
	
	public Client(String host) {
		this.host=host;
	}
	
	
	public void 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;
		}
	}

	public void closeConnection() {
		close=true;
		try {
			if (socket!=null)
				socket.close();
		} catch (IOException ioe) {}
	}

	public void deleteListener(Listener listener) {
		if (listener!=null && this.listeners.contains(listener))
			this.listeners.remove(listener);
	}

	public void newListener(Listener listener) {
		if (listener!=null && !this.listeners.contains(listener))
			this.listeners.add(listener);
	}


	public synchronized void 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
	 */
	public void localSend(Object o) {
		for (Iterator iter = listeners.iterator(); iter.hasNext();) {
			Listener element = (Listener) iter.next();
			if (element!=null) {
				element.received(o);
			}
		}
	}

	public void startConnection() {
		try {
		} catch (Exception e) {e.printStackTrace();}
		new Thread(this).start();

	}
	
	public boolean isConnected() {
		return connected;
	}

}
This is the server:
public class Server extends Client implements Runnable {
	private final static int 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;
	private static 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 
	}
	
	public void 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
	 */
	public synchronized void send(Object o,Socket socket) {
		ObjectOutputStream oos =null;
		
		if (socket!=null && (socket instanceof Socket) && socket.isConnected()) {
			try {
				oos = ooss.get(socket);
      } catch (Exception e) {
			}
			try {
	      oos.writeObject(o);
	      oos.flush();
	      oos.reset();
			} catch(Exception e) {
			}
		} 
	}

	public void 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
	 */
	public void localSend(Object o, Socket socket) {
		if (o!=null && o instanceof 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);
			}
		}
	}
	
	public void 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):
public class ServerThread extends Thread {
	private Server server;
	private Socket socket;
	private int numRetries=3;
	private ObjectInputStream ois;
	
	public ServerThread(Socket socket, ObjectInputStream ois, Server server) {
		this.socket=socket;
		this.ois=ois;
		this.server=server;
	}
	
	public void 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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 31 2008
Added on May 27 2007
17 comments
4,317 views