2 Replies Latest reply on Jun 25, 2007 12:41 AM by EJP

    Socket, thread and Corrupted ObjectInputStream

    843790
      Hello,

      i'm trying to implement a client/server code where the clients can send serialized Objects while reading incoming ones from a server.

      To simplify the example i only serialize String (But i plan to serailze more complez object, so i want to use Object serialisation)

      here is what i've done

      the client:
      public class GameClient extends Thread
      {
           private ObjectOutputStream m_oos = null;
           private ObjectInputStream m_ois = null;
           private Socket m_socket = null;
           public static void main (String[] argv)
           {
                EchoServer s=null;
                GameClient c=null;
                try
                {
                     Trace.initTraces(argv);
                     s =new EchoServer(2564);
                     c = new GameClient("localhost",2564);
                     int counter=0;
                     while(true && s.isAlive() && c.isAlive())
                     {
                          counter++;
                          //System.out.println("Message #"+counter);
                          long time = System.currentTimeMillis();
                          c.m_oos.writeObject(new String("toto"));
                          c.m_oos.flush();
                          c.m_oos.reset();
                          time = System.currentTimeMillis()-time;
                          //System.out.println("Message Sent to the server in "+time+" ms.");
                     }
                }catch(Exception e)
                {
                     e.printStackTrace();
                     if (c!=null)
                          c.stop();
                     if (s!=null)
                          s.stop();
                }
           }
           
           
           public GameClient(String serverName,int port) throws IOException, UnknownHostException
           {
                m_socket = new Socket(serverName, port);
                m_oos = new ObjectOutputStream(m_socket.getOutputStream());
                m_ois = new ObjectInputStream(m_socket.getInputStream());
                start();
           }
      
           
           public void run()
           {
      
                System.out.println("Listening Client Thread started");
                while(true)
                {
                     try
                     {
                          String serverMessage = (String) m_ois.readObject();
                          //System.out.println("Message red:"+serverMessage);
                     }
                     catch(Exception e)
                     {
                          e.printStackTrace();
                          stop();
                     }
                }
           }     
      and here is the server code. for the example it just echo the client
      public class EchoServer extends Thread
      {
           private ServerSocket m_socket;
      
           public EchoServer(int port) throws Exception
           {
                m_socket = new ServerSocket(port);
                this.start();
           }
      
           public void run()
           {
                try
                {
                     Socket client = m_socket.accept();
                     ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
                     ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
                     
                     while (true)
                     {
                          Object o = ois.readObject();
                          oos.writeObject(o);
                          oos.flush();
                          oos.reset();
                     }
                }
                catch (Exception e)
                {
                     e.printStackTrace();
                     stop();
                }
           }
      }
      and here is the stack trace


      java.io.StreamCorruptedException
           at java.io.ObjectInputStream.readObject0(Unknown Source)
           at java.io.ObjectInputStream.readObject(Unknown Source)
           at comtst.GameClient.run(GameClient.java:65)
      java.io.StreamCorruptedException
           at java.io.ObjectInputStream.readObject0(Unknown Source)
           at java.io.ObjectInputStream.readObject(Unknown Source)
           at comtst.EchoServer.run(EchoServer.java:30)
      java.net.SocketException: Software caused connection abort: socket write error
           at java.net.SocketOutputStream.socketWrite0(Native Method)
           at java.net.SocketOutputStream.socketWrite(Unknown Source)
           at java.net.SocketOutputStream.write(Unknown Source)
           at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
           at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source)
           at java.io.ObjectOutputStream.writeNonProxyDesc(Unknown Source)
           at java.io.ObjectOutputStream.writeClassDesc(Unknown Source)
           at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
           at java.io.ObjectOutputStream.writeObject0(Unknown Source)
           at java.io.ObjectOutputStream.writeFatalException(Unknown Source)
           at java.io.ObjectOutputStream.writeObject(Unknown Source)
           at comtst.GameClient.main(GameClient.java:31)


      Do you have any idea why it is not working
        • 1. Re: Socket, thread and Corrupted ObjectInputStream
          843790
          I am actually having the same problem with my program. CorruptedStreams. But anyway, you should try putting buffers on those.
          ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
          
          ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
          Sorry, that's all i can do for now. Let me know if you find anything out. Cheers!
          • 2. Re: Socket, thread and Corrupted ObjectInputStream
            EJP
            I agree but it won't solve the problem.

            But the problem isn't a corrupted stream, it's the reset socket. And that in turn is probably just due to the hard loop and running out of system buffers. Can you slow it down a little, or make it into a more realistic protocol?

            Also don't call Thread.stop(). You can just return from the run() method. But you should always close the socket, or better still its output stream, on any IOException.