2016-09-29 25 views
-1

我正在嘗試在Java應用程序上設置套接字超時。一些開發人員在OS X上,其他人在Windows上。問題是在Windows機器上,我們得到一個SocketException:連接在2分鐘後重置,無論超時設置爲什麼。但是,在OS X上,它完全按照預期工作。Java setSoTimeout在Windows上不工作

這似乎是JVM與基礎Windows套接字庫進行交互的具體問題。無論如何要解決這個問題。

這是創建套接字的代碼片段。

protected Socket openSocket() throws UnknownHostException, IOException { 

    Socket socket = new Socket(); 
    SocketAddress endpoint = new InetSocketAddress(this.getHost(), this.getPort()); 
    try { 
     if (this.getConnectTimeout() != null) { 
     socket.connect(endpoint, this.getConnectTimeout()); 
     } else { 
     socket.connect(endpoint); 
     } 
    } catch (ConnectException ex) { 
     throw ex; 
    } catch (IOException ex) { 
     ConnectException connEx = new ConnectException( 
      String.format("Failed to connect to service at %s:%d. Reason: %s", 
       this.getHost(), this.getPort(), ex.getMessage())); 
     connEx.initCause(ex); 
     throw connEx; 
    } 

    logger.debug("Socket opened to {}:{}", this.getHost(), this.getPort()); 

    if (this.getResponseTimeout() != null) { 
     socket.setSoTimeout(this.getResponseTimeout()); 
    } 

    return socket; 
    } 

代碼調用openSocket然後調用read返回的插座上。

回答

-1

讀超時不會導致連接重置。它們導致SocketTimeoutExceptions(以Java)。

連接重置因此不是證明設置讀取tiemout不起作用。這可能是其他事情的證據,例如防火牆規則。

你的猜想是錯誤的。

+0

@downvoter在上面的一些錯誤? – EJP

-1

的問題是在調用方法

StringBuffer responseText = new StringBuffer(); 
try (Socket socket = openSocket(); 
     OutputStream os = socket.getOutputStream(); 
     PrintStream psOut = new PrintStream(os, true); 
     BufferedReader inReader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));) { 
    logger.trace("Opened socket"); 
    try { 
    psOut.println(requestXML); 
    } finally { 
     socket.shutdownOutput(); 
    } 

這導致FIN_WAIT發送下面的代碼,並出現兩分鐘後自動的Windows殺死插座,這種地位。然後當發生讀取時,它會導致SocketException:Connection重置。

+0

FIN_WAIT未發送。 FIN發送。如果您有一些獨立的證據表明「Windows在兩分鐘後自動將套接字處於該狀態」請提供。如果這是真的'shutdown()'SHUT_WR或多或少沒有用處。 – EJP

相關問題