3

I have a server with many clients.. Every connection arrives to the server

if it's accepted, I send it to a thread:

server= serverSocketcht.accept();
new ThrdConv(server).start(); 

in the ThrdConv thread I set the input stream and output stream to this new conection

    this.OOS=new ObjectOutputStream(server.getOutputStream());
    this.OIS=new ObjectInputStream(server.getInputStream());

then I store the arrived connection, (lets call it new client) in a list of clients:

  if(isLogged){ // if success login!
      thsisUser= new Clientuser(server,OOS,OIS,Omsg.my_gender,Omsg.userID);
        boolean IsAdded= EIQserver.OnlineusersList.add(this.thsisUser);

everything works fine and the Clients can send messages and chat with other clients...

The problem is when a client leaves, I get this Exception :

SEVERE: null
java.io.EOFException
at    
java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2571)

here is my Leave function:

 Iterator<Clientuser> iterator = EIQserver.OnlineusersList.iterator();
   if(EIQserver.OnlineusersList.size()>=1)
 Omsg.type= MessageType.Leave;


   sendMessage(OLeavemsg); // tell the partner that I am leaving...

       while (iterator.hasNext()) {
         Clientuser next = iterator.next();
         if (next.ID.equals(OLeavemsg.userID)) 
         {
            next.ClientPort.shutdownInput(); // ClientPort is a socket of this Client
            next.ClientPort.shutdownOutput();
           iterator.remove();// remove the partner
         }
         break;
     }     
       // end leave////////////////////////////////////////////////

The connection is removed from the list, but the above exception stops the Server...

help me get rid of this complex problem

spydon
  • 9,372
  • 6
  • 33
  • 63
EsmaeelQash
  • 488
  • 2
  • 6
  • 20

3 Answers3

0

You should close this.OOS and this.OIS. They will close inner streams recursively. in you current case outer streams fail because client is closed first. You can examine Object*Stream, their close method close inner stream too.

Mikhail
  • 4,175
  • 15
  • 31
  • You don't need to close both the OOS and the OIS. Closing either closes both the other stream and the socket. Closing the OOS is what is recommended, but unblocking the pending read with a shutdownInout() is perfectly legitimate, *provided* you handle the resulting exception correctly. He isn't. That's the only problem here. – user207421 Nov 06 '13 at 13:03
0

thank you Mikhail, your answer was the key of the solution. And for the other readers,I will Describe how did I solve this:

  • first I closed the OOS,OIS... as you advice..
  • secondly I stop the thread.. How do I stop the thread? :
    • I declared new boolean variable named "Running" and set the condition for the main loop of the thread to while(running) and when I want to stop the main loop of the thread I set running=false this stop the the use of the closed streams!!
EsmaeelQash
  • 488
  • 2
  • 6
  • 20
  • Welcome, using interrupts is a better way to stop thread http://stackoverflow.com/a/10962613/2031799 – Mikhail Nov 06 '13 at 12:29
  • @Mikhail Not when there's a blocked pending read in a java.net class it isn't. The behaviour is undefined. – user207421 Nov 06 '13 at 13:03
0

You shutdown the input, you got EOFException when reading the input. That's exactly what's supposed to happen. You have to catch EOFException anyway when reading an ObjectInputStream. There is no 'complex problem' here at all. Just poor exception handling.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • This is actually a rule of thumb to close the otter-most stream and not to produce exceptions. – Mikhail Nov 06 '13 at 13:39
  • It is a rule of thumb to close the outermost *output* stream. If you want to avoid exceptions, you can't close the input stream while another thread is reading it either, as that will also throw an exception, and a more severe one than EOFException, which has to be catered for anyway. I personally don't have exception phobia myself. – user207421 Nov 06 '13 at 13:44
  • EJP, thanks, I must say that you helped me very much by notify me to not ignore handling EXceptions... – EsmaeelQash Nov 07 '13 at 12:36