3 Replies Latest reply on Dec 19, 2008 11:29 PM by EJP

    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