【问题标题】:java.net.SocketException: Socket closed on CentOS (Linux)java.net.SocketException: CentOS (Linux) 上的套接字关闭
【发布时间】:2023-03-10 00:51:02
【问题描述】:

我有一个 java 应用程序,它使用内部代理服务器来分析 Web 应用程序(在浏览器中 - Prism 或 Chrome/Chromium 中)的 http 流量,这实际上是同一应用程序的一部分。根据流量分析,它会执行一些操作...

该应用程序在 Windows(XP、7 和 8)中运行良好,我也在 Ubuntu 和 Suse 中使用它,没有任何问题。最近一个新的潜在客户想在 CentOs 中测试它,但我得到了 java.net.SocketException: Socket closed。 以下代码产生此异常(实现 Runnable):

   public void run() {
    try {
        // create a server socket, and loop forever listening for
        // client connections
        synchronized (this) {
            server = new ServerSocket(thisPort);
            notifyAll();
        }
        while (true) {
            if(server.isClosed()){ // I added this just to see if it helps...
                server = new ServerSocket(thisPort);
            }
            Socket client = server.accept();  // Exception thrown here

            ProxyThread t = new ProxyThread(client, fwdServer, fwdPort); // new Thread takes care of comunication, no issues there at all...
            t.setDebug(debugLevel, debugOut);
            t.setTimeout(ptTimeout);
            t.start();
        }
    } catch (Exception e) {
            debugOut.println("Proxy Thread error: " + e);
            e.printStackTrace();

    }

    closeSocket();
}

追踪:

java.net.SocketException: Socket closed
    at java.net.PlainSocketImpl.socketAccept(Native Method)
    at java.net.AbstractPlainSocketImpl.accept(Unknown Source)
    at java.net.ServerSocket.implAccept(Unknown Source)
    at java.net.ServerSocket.accept(Unknown Source)
    at sk.tido.simpleproxy.Proxy.run(Proxy.java:20)

系统信息:

CentOS release 6.3 (Final)
Linux pcpanel 2.6.32-279.el6.x86_64 #1 SMP Fri Jun 22 12:19:21 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

我用 2 个版本的 java 对其进行了测试:

java version "1.7.0_60"
Java(TM) SE Runtime Environment (build 1.7.0_60-b19)
Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)

java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.1) (rhel-1.45.1.11.1.el6-x86_64)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode

欢迎提出任何建议,因为应用程序在其他 Linux 发行版中已经运行了近 2.5 年,没有出现任何问题... 我只能在网上找到和我类似的问题,但与httpclient有关,对我帮助不大。

【问题讨论】:

    标签: java linux sockets proxy


    【解决方案1】:

    当此代码在 accept() 中被阻止时,您正在应用程序的其他地方关闭 ServerSocket。

    【讨论】:

    • 您的回答让我从其他整体的角度审视了代码。我发现其他线程关闭了套接字。但这不是我的代码中的错误,它按设计工作。后来我发现该平台上的 Chromium 存在问题,并且不会从那里开始。所以;我的代码无法启动它,并且线程负责检查浏览器是否正在运行已结束的代理线程(关闭的套接字)。然后整个应用程序正常关闭...我在错误的地方寻找错误,我不认为 Chromium 可能是问题...
    猜你喜欢
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 2014-08-28
    • 1970-01-01
    相关资源
    最近更新 更多