Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中安全关闭套接字?因为我遇到了一个异常,我做错了什么?_Java_Sockets_Networking - Fatal编程技术网

如何在Java中安全关闭套接字?因为我遇到了一个异常,我做错了什么?

如何在Java中安全关闭套接字?因为我遇到了一个异常,我做错了什么?,java,sockets,networking,Java,Sockets,Networking,服务器: 客户: @Override public void run() { Client client; try { while ((client = (Client) ois.readObject()) != null && !Thread.currentThread().isInterrupted() && threads.containsKey(sock)) {

服务器:

客户:

        @Override
    public void run() {
        Client client;
        try {
            while ((client = (Client) ois.readObject()) != null && !Thread.currentThread().isInterrupted() && threads.containsKey(sock)) {
                if (keyFound(client.getKEY()) && idFound(client.getID()) && inetAddrFound(sock.getInetAddress().getHostName())) {
                    oos.writeObject(tab);
                    oos.flush();
                    threads.remove(sock);
                    Thread.currentThread().interrupt();
                } else {
                    oos.writeObject(new UserNotFound(keyFound(client.getKEY()), idFound(client.getID()), inetAddrFound(sock.getInetAddress().getHostName())));
                    oos.flush();
                    threads.remove(sock);
                    Thread.currentThread().interrupt();
                }
            }
        } catch (Exception e) {
            Spooker.get().getWindow().append(sock.getInetAddress().getHostAddress() + " has disconnected.");
            threads.remove(sock);
            try {
                sock.close();
            } catch (IOException e1) {
                e1.printStackTrace();

            Thread.currentThread().interrupt();
        }
    }
}
当我试图关闭插座时,我得到了这个-

java.net.SocketException:套接字已关闭


我正在检查套接字是否在while循环发生之前关闭-那么我做错了什么?我基本上想停止线程并关闭连接(停止处理客户端的客户端线程和服务器线程)。

既然在while循环中关闭了套接字,为什么还要使用while循环?它不会运行一次以上。@Kayaman我需要等待对象被接收,然后关闭连接-否则我该怎么做?好吧,那代码完全是胡说八道。由于
readObject()
是一个阻塞调用,因此如果要读取单个对象,则不需要整个循环。所有那些
interrupt()
调用也都是胡说八道。我严肃地质疑这种“把随机的东西放进去,希望它能工作”的编程风格。
系统退出(1)
也很有问题。啊,我知道你在用
System.exit()
做什么。@Kayaman我正在中断线程以“杀死”它。我需要合上插座。我只是假设你会这么做。
 @Override
    public void run() {
        try {
            while ((object = ois.readObject()) != null && !Thread.currentThread().isInterrupted() && !socket.isClosed()) {
                if (object instanceof SpookerTab) {
                    tab = (SpookerTab) object;
                    Spooker.getWindow().setTab(tab);
                } else if (object instanceof UserNotFound) {
                    notFound = (UserNotFound) object;
                    if (!notFound.isIpFound()) {
                        JOptionPane.showMessageDialog(null, "IP not found.");
                    } else if (!notFound.isIdFound()) {
                        JOptionPane.showMessageDialog(null, "CPUID not found.");
                    } else if (!notFound.isKeyFound()) {
                        JOptionPane.showMessageDialog(null, "You aren't using the current version.");
                    }

                    Thread.currentThread().interrupt();
                    ois.close();
                    oos.close();
                    socket.close();
                    // run other file, delete this file
                    System.exit(1);
                }

                Spooker.get().createWindow();
                ois.close();
                oos.close();
                Thread.currentThread().interrupt();
                System.out.println("interuped: " + Thread.currentThread().isInterrupted());
                socket.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }