问题
I'm downloading a big file (say around 100mb) and I'm receiving SocketException: Read timed out
every now and then.
I'm thinking of raising the socket timeout. Actually, I'm thinking of setting the socket timeout to 0 (infinite) as eventually the sizes that the files that my app will be downloading may even go greater than 300mb, or even greater than 300mb. Is this a good practice?
Regarding socket timeout, when does the timeout countdown actually starts? I mean, when a socket timeout occurs, does it mean that the connection is still alive and file is still continously being downloaded but just timeouts because a socket timeout is configured? Or does the countdown starts when it figures that connection is still alive but no data is being sent by the server; thus countdown begins and reached the timeout?
Because if the case is the latter, then I'll not opting to go to infinite, since it will be caused by the server not sending me data and not by my app.
回答1:
I'm downloading a big file (say around 100mb) and I'm receiving SocketException: Read timed out every now and then.
So your read timeout is too short, or the peer isn't responsive enough for your needs.
I'm thinking of raising the socket timeout.
From what?
Actually, I'm thinking of setting the socket timeout to 0 (infinite) as eventually the sizes that the files that my app will be downloading may even go greater than 300mb, or even greater than 300mb. Is this a good practice?
The read timeout has nothing whatsoever to do with the download size. It has to do with the expected service time of a request. I generally recommend setting it to double the expected service time. The purpose of a read timeout is to tell you when the peer isn't responding, after all.
Regarding socket timeout, when does the timeout countdown actually starts?
When you enter the read method.
I mean, when a socket timeout occurs, does it mean that the connection is still alive
Yes.
and file is still continously being downloaded
No. No data has arrived within the timeout period.
but just timeouts because a socket timeout is configured?
The read times out because (a) you set a read timeout and (b) it expired. Don't overthink this.
Or does the countdown starts when it figures that connection is still alive but no data is being sent by the server; thus countdown begins and reached the timeout?
See above.
Because if the case is the latter, then I'll not opting to go to infinite, since it will be caused by the server not sending me data and not by my app.
The read timeout is caused by data not arriving within the timeout period configured by the receiving application. The timeout could be too short, which is the application's fault, or it could be because the peer didn't send anything, which is the peer's fault, or both. It isn't possible to decide a priori.
来源:https://stackoverflow.com/questions/22449647/socket-read-timed-out-should-i-go-for-0-inifite