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

4

3 回答 3

0

谢谢米哈伊尔,您的回答是解决方案的关键。对于其他读者,我将描述我是如何解决这个问题的:

  • 首先,我按照您的建议关闭了 OOS、OIS ......
  • 其次我停止线程..我如何停止线程?:
    • 我声明了名为“Running”的新布尔变量,并将线程主循环的条件设置为 while(running),当我想停止线程的主循环时,我设置 running=false 这停止使用已关闭流!!
于 2013-11-06T12:21:52.457 回答
0

您关闭输入,读取输入时收到 EOFException 。这正是应该发生的事情。在读取 ObjectInputStream 时,无论如何您都必须捕获 EOFException。这里根本不存在“复杂问题”。只是糟糕的异常处理。

于 2013-11-06T13:00:09.393 回答
0

您应该关闭 this.OOS 和 this.OIS。他们将递归地关闭内部流。在您当前的情况下,外部流失败,因为客户端首先关闭。您也可以查看 Object*Stream,它们的 close 方法也关闭内部流。

于 2013-11-06T11:26:15.077 回答