SocketTimeoutException when ConnectTimeout and ReadTimeout is infinite? [duplicate]

蓝咒 提交于 2019-12-25 08:25:04

问题


Possible Duplicate:
Receiving request timeout even though connect timeout and read timeout is set to default (infinite)?

I tried to connect to a web service and received a SocketTimeoutException after approximately 20 seconds. The Tomcat server hosting the web service is down so the Exception is expected. However, I did not set the value of my ConnectTimeout and ReadTimeout. According to the documentation, the default values of these two are infinite.

One possibility for this is that the server I tried connecting to has its own timeout. But when my friend tried to connect to it using iOS, his connection timed out after approximately 1 minute and 15 seconds. If the server is the one issuing the timeout, our connection should have timed out at almost the same time. Please note that he is also using the default time out of iOS.

  • Why did my socket timed out so early when my connect and read timeout are set to infinite?
  • Is socket timeout different to connect and read timeout? If so, how is it different?
  • How can I know the value of my socket timeout? I am using HttpURLConnection.
  • Is there a way to set the socket timeout? How?

Below is a snippet of my code:

httpURLConnection = (HttpURLConnection) ((new URL("http://www.website.com/webservice")).openConnection());
httpURLConnection.setDoInput(isDoInput);
httpURLConnection.setDoOutput(isDoOutput);
httpURLConnection.setRequestMethod(method);

try
{
    OutputStreamWriter writer = new OutputStreamWriter(httpURLConnection.getOutputStream());
    writer.write("param1=value1");
    writer.flush;
}catch(Exception e)
{

}

回答1:


Why did my socket timed out so early when my connect and read timeout are set to infinite?

Code please.

Is socket timeout different to connect and read timeout? If so, how is it different?

SocketTimeoutException is a read timeout.

How can I know the value of my socket timeout? I am using HttpURLConnection.

HttpURLConnection.getReadTimeout(); also HttpURLConnection.getConnectTimeout().

Is there a way to set the socket timeout? How?

HttpURLConnection.setReadTimeout().

You have already cited all these methods in your original post. Why are you asking about them here?




回答2:


Finally, I found what causing my timeout! It turns out that it is indeed the server who is causing my timeout. I doubt on this one at first because I am receiving a different timeout when using iOS which is more than 1 minute.

So here it is: The operating system holding my Tomcat server is Windows. Windows' default number of retries for unanswered connection is 2. So when your first attempt to connect fails, you still have 2 retries left. The retries are all done internally. I'm not sure how the time for each retry is calculated but basically it's 3 + 6 + 12 = 21 seconds.

  • 1st retry = 3 seconds
  • 2nd retry = 6 seconds
  • 3rd retry = 12 seconds

After the 3rd retry, your connection will be cut-off. Also, by that time, you already waited for 21 seconds.



来源:https://stackoverflow.com/questions/10492406/sockettimeoutexception-when-connecttimeout-and-readtimeout-is-infinite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!