Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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!!

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top