0

在类 SonarLint 和 sonarqube 服务器之一的 eclipse 中说套接字没有关闭,即使我关闭了它。

财政年度: 在此处输入图像描述

关闭套接字的最佳方法是什么?谁能指导我。

注意:如果我在同一个 try 块中没有 out = new ObjectOutputStream(...) 代码,我不会收到此错误。

4

2 回答 2

0

使用 try-with-resources 语句,例如:

            try (ObjectOutputStream out =
                           new ObjectOutputStream(socket.getOutputStream()) {
               .....
            }
于 2017-12-04T06:36:14.603 回答
0

这是工作代码:

    try (Socket socket=new Socket(ipAddress, port);
            ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream())) {
           //this is how you can use socket & out
        out.write(null);
        socket.getInetAddress();
    }
于 2017-12-04T08:11:46.773 回答