Strange connection time-out using winsock
I've been stuck debugging my custom downloader all morning now and I finally know where it goes wrong but I don't know why.
Let me explain: I'm coding a HTTP downloader just using the basic winsock functions, not using any fancy class or framework. I don't mind if it blocks because it is already running in a separate thread so actually I just use the example code on MSDN:
http://msdn.microsoft.com/en-us/library/ms737591(v=VS.85).aspx
Sending the request:
GET /index.html HTTP/1.0\r\n
Host: www.example.com\r\n
User-Agent: MyCustomDownloader/1.0\r\n
\r\n
\r\n
And in the receive loop I do some allocation, parse header lines, open file handle, etc. So it does not rece开发者_开发知识库ive all in one go, it takes some time. But I suppose that shouldn't be a problem right? I receive in blocks of 4096 bytes.
All is fine but sometimes recv returns -1 before it returns 0. And when I call WSAGetLastError() it returns 10060 indicating:
WSAETIMEDOUT
Connection timed out.
A connection attempt failed because the connected party did not properly
respond after a period of time, or the established connection failed
because the connected host has failed to respond.
First I thought it was the host, but I observe the behavior with any host...
Is this normal for HTTP connections that they just drop-out from time to time? Am I doing something wrong?
I bet you arr not getting 10060 from recv. You are getting it from the connect function and the problem may be at specifying the host address.
精彩评论