SSL 连接超时和读取超时

发布于 2024-12-09 08:47:13 字数 1627 浏览 0 评论 0原文

我对在代码中设置套接字超时的位置有疑问。我想要实现的是,当创建套接字时,超时应该为 10 秒。所以我在握手之前设置它。现在我在日志中看到的错误有两种。 1) 连接超时错误和 2) 读取超时错误。所以我想知道是否有人可以向我解释更多关于在哪里设置超时的信息。我有以下代码:

try{
  SSLSocketFactory factory=(SSLSocketFactory)SSLSocketFactory.getDefault();
  socket = (SSLSocket)factory.createSocket(host,port);
  socket.setSoTimeout(10000);
  socket.startHandshake();

  OutputStream socketOut =( socket.getOutputStream() );

  String command = method+" "+path+" HTTP/1.0\n";
  //command+="Host: "+host+"\nConnection: close\n";

  if (data!=null) {
command+="Content-Length: " + data.length +"\n\n";
    sendBytes.write(command.getBytes(),0,command.length());
    sendBytes.write(data,0,data.length);
  } else {
    // if data == null then we are most likely doing a GET request
    sendBytes.write(command.getBytes(),0,command.length());
  }

  sendBytes.write("\n".getBytes(),0,"\n".length());

  temp = sendBytes.toByteArray();
  socketOut.write(temp,0,temp.length);
  socketOut.flush();

  /* read response */
  BufferedInputStream socketIn = 
    new BufferedInputStream( socket.getInputStream() );

  byte[] inputData = new byte[READSIZE];
  while ((bytesRead=socketIn.read(inputData,0,READSIZE)) > -1) {
      receiveBytes.write(inputData,0,bytesRead);
  }
  result=receiveBytes.toByteArray();

  //receiveBytes.close();
  //sendBytes.close();
  //socket.close();

} catch (Exception e) {
  throw e;
}finally {
try { receiveBytes.close(); sendBytes.close(); socket.close(); } catch (Exception ee) {}
}     
return result;
} // end submit

} // end class

请让我知道如何让超时至少起作用。在日志中,错误发生在 18 秒(不应该是这种情况)而不是 10 秒。

谢谢

I have a doubt on where to set the socket timeout within my code. What I am trying to achieve is that when the socket is created the timeout should be 10seconds. So I am setting it before the handshake. now the error that I see in the logs are of two kinds. 1) Connection Timeout Error and 2) Read timeout Error. So I was wondering if anyone could explain me more about where to set the timeouts. I have the following code:

try{
  SSLSocketFactory factory=(SSLSocketFactory)SSLSocketFactory.getDefault();
  socket = (SSLSocket)factory.createSocket(host,port);
  socket.setSoTimeout(10000);
  socket.startHandshake();

  OutputStream socketOut =( socket.getOutputStream() );

  String command = method+" "+path+" HTTP/1.0\n";
  //command+="Host: "+host+"\nConnection: close\n";

  if (data!=null) {
command+="Content-Length: " + data.length +"\n\n";
    sendBytes.write(command.getBytes(),0,command.length());
    sendBytes.write(data,0,data.length);
  } else {
    // if data == null then we are most likely doing a GET request
    sendBytes.write(command.getBytes(),0,command.length());
  }

  sendBytes.write("\n".getBytes(),0,"\n".length());

  temp = sendBytes.toByteArray();
  socketOut.write(temp,0,temp.length);
  socketOut.flush();

  /* read response */
  BufferedInputStream socketIn = 
    new BufferedInputStream( socket.getInputStream() );

  byte[] inputData = new byte[READSIZE];
  while ((bytesRead=socketIn.read(inputData,0,READSIZE)) > -1) {
      receiveBytes.write(inputData,0,bytesRead);
  }
  result=receiveBytes.toByteArray();

  //receiveBytes.close();
  //sendBytes.close();
  //socket.close();

} catch (Exception e) {
  throw e;
}finally {
try { receiveBytes.close(); sendBytes.close(); socket.close(); } catch (Exception ee) {}
}     
return result;
} // end submit

} // end class

Please let me know how can i get the timeout to atleast work . In the logs the errors are occuring at 18secs ( which should not be the case) instead of 10secs.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的位置 2024-12-16 08:47:13

问题很可能是在创建套接字时连接时已经发生超时。尝试首先使用无参数工厂方法实例化未连接的套接字,然后使用 Socket#connect 除了 Socket#setSoTimeout 之外还带有 timeout 选项。后者仅对这些操作起作用

ServerSocket.accept()

SocketInputStream.read()

DatagramSocket.receive()

但在连接尝试期间不处理超时。

有关更多详细信息,另请参阅有关网络超时的技术文章

The problem is most likely that the timeout already occurs upon connection when creating your socket. Try instantiating an unconnected socket first with the no arguments factory method and then use Socket#connect with the timeout option in addition to Socket#setSoTimeout. The latter only kicks in for these operations

ServerSocket.accept()

SocketInputStream.read()

DatagramSocket.receive()

but does not handle timeouts during connection attempts.

See also the technical article for networking timeouts for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文