3 Replies Latest reply: Dec 19, 2008 5:29 PM by EJP RSS

    ObjectInputStream & ObjectOutputStream with Sockets

    807589
      Hello,
      i am trying to biuld a Chat-System also :


      in Client Side i have the class : Client.java an applet:
                                     try {
                     socket = new Socket( host, port );
                     din    = new ObjectInputStream( socket.getInputStream() );
                     dout   = new ObjectOutputStream( socket.getOutputStream() );
                     dout.writeObject(this.user);
                     timer =new Thread( this );
                     timer.start();
                } catch (UnknownHostException e) {
                     e.printStackTrace();
                } catch (IOException e) {
                     e.printStackTrace();
                }
      that works fine.


      in Server Side i have two classes Server.java and ServerThread.java ,
      in Server.java i have :
      try {
                     ss = new ServerSocket( port );
                     log( "Server started and listening on channel "+port );
                     while (ss!=null && !ss.isClosed()) {
                          Socket s = ss.accept();
                          ObjectOutputStream dout = new ObjectOutputStream( s.getOutputStream() );
                          ObjectInputStream din = new ObjectInputStream( s.getInputStream() );
                          Object inO = din.readObject();
                          User u=new User();
                          if(inO != null) {
                               u=(User)inO;
                               dout.writeObject(u); // i tell the Client about the new User, i tested it and the client recieved it corecctly
                               log("new incoming user: "+inO.toString());
                               new ServerThread( this,s,u ); 
                          }
                     }
      
                } catch (IOException e) {
                     e.printStackTrace();
                } catch (ClassNotFoundException e) {
                     e.printStackTrace();
                }
      and thats works fine but in ServerThread.java i have the following :



      public void run() {
                try {
                     ObjectInputStream  din = new ObjectInputStream (socket.getInputStream() ); // exactly here goes something wrong
                     while (true) {
                          Object inO = din.readObject();
                          if(inO != null) {
                               log(inO.toString());
                               if(inO instanceof User){
                                    User u = new User();
                                    u=(User)inO; 
                                    log(u.getNick());
                               }
                          }else{
                               break;
                          }
                     }
                } catch( IOException ie ) {
                     ie.printStackTrace();
                } catch (ClassNotFoundException e) {
                     e.printStackTrace();
                } finally {
                     //server.removeConnection( socket );
                     log("User ["+u.getNick()+"] Left the Chat");
                }
           }
      if i send a new Object "User" from Client to server its throwing an error exactly at this line :
      ObjectInputStream  din = new ObjectInputStream (socket.getInputStream() ); 
      error :

      java.io.StreamCorruptedException: invalid stream header: 71007E00
           at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
           at java.io.ObjectInputStream.<init>(Unknown Source)
           at ps.java.o.server.ServerThread.run(ServerThread.java:27)

      any idea whats going wrong ???

      thanks,
      majd

      Edited by: am05011996 on Dec 18, 2008 10:38 PM
        • 1. Re: ObjectInputStream & ObjectOutputStream with Sockets
          807589
          A couple of suggestions;
          - always create the OutputStream first and flush it before creating the input stream. The is because the ObjectInputStream reads some data when it is created. (And it will block until it gets it)
          - Never attach more than one reading or writing stream to a socket. You are creating two ObjectInputStreams, the first reads the valid header, the second reads some random piece of data.

          In your case, Server.java doesn't need an ObjectOutputStream move it to ServerThread. More importantly, once you have created the ObjectInputStream in Server you need to pass this to ServerThread as you cannot create another one.
          • 2. Re: ObjectInputStream & ObjectOutputStream with Sockets
            807589
            thank you sooo much.
            i had just to pass ObjectInputStream to the ServerThread and its working now .
            thanks.
            majd
            • 3. Re: ObjectInputStream & ObjectOutputStream with Sockets
              EJP
              Also, don't do I/O in the accept loop. This is poor practice - if the client is misbehaving it can stop your server from accepting new connections. Do all the I/O for a connection in the thread you start for the connection.