套接字 - 在服务器断开连接时断开客户端连接

时间:2016-04-05 02:34:45

标签: java sockets

我正在用Java中的套接字编写聊天应用程序。我有这种方法被调用多次,因为有客户端连接到服务器。

public class ClientInfo implements Serializable
{
    public ObjectOutputStream writer;
    public ObjectInputStream reader;
    public String user_name;

    public ClientInfo(ObjectOutputStream writer, ObjectInputStream reader, String name)
    {
        this.reader = reader;
        this.writer = writer;
        this.user_name = name;
    }
}//end class
 /*Client side code*/
  void remove_online_user(ClientInfo client_to_remove)
        {
            try
            {
                client_to_remove.reader.close(); //this should trigger server handler in client
                client_to_remove.writer.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }//end remove_online_users()

在客户端,我创建了一个像这样的ClientInfo对象

client = new ClientInfo(
                    new ObjectOutputStream(client_socket.getOutputStream()),
                    new ObjectInputStream(client_socket.getInputStream()),
                    user_name);

 new ServerHandler(client, this);

最后,ServerHandler(抱歉粘贴所有代码,我尽力尽可能地压缩它)

class ServerHandler implements Runnable
{
    private ClientInfo server;
    private ClientBackEnd client_back_end;


    public ServerHandler(ClientInfo server, ClientBackEnd client_back_end)
    {
        this.server = server;
        this.client_back_end = client_back_end;
    }//end constructor


    /*method invoked by start() call in constructor, manages communication between client and server*/
    public void run()
    {
        try
        {
            MessageInfo message;
            while ((message = (MessageInfo) server.reader.readObject()) != null)
            {
                if (message.clients != null)
                {
                    for (ClientInfo cd : message.clients)
                    {
                        if (client_back_end.listModel.contains(cd)) continue;
                        client_back_end.listModel.addElement(cd);
                    }

                    for (Object cd : client_back_end.listModel.toArray())
                    {
                        if (message.clients.contains(cd)) continue;
                        client_back_end.listModel.removeElement(cd);
                    }

                }
                else
                {
                   System.out.println("\n" + message.message_contents);
                }
            }
        }
        catch (ClassNotFoundException | IOException e)
        {
            //This should trigger during the execution of remove_online_user();
            client_back_end.disconnect_from_server(); 
        }
    }//end run()

我的目标::

当用户停止服务器时,必须断开所有客户端。在remove_online_user()期间,关闭ObjectInputStream / ObjectOutputStream应该在客户端服务器处理程序中触发异常。但它目前没有。我做错了什么?

(如果我没有发布足够的信息,我提前道歉;我不想让任何人用4个完整的.java文件大肆宣传。如果需要更多信息,请告诉我。)

1 个答案:

答案 0 :(得分:0)

关闭套接字会在对等方EOFException方法中触发readObject()

出于同样的原因,您的读取循环无效。 readObject()在流的末尾不会返回null。