InternetReadFile gets stuck in second loop
Hi,
I am reading data using WinINet and InternetReadFile. The function works perfectly fine, but during the second loop, InternetReadFile takes around 30-60 seconds to return data. The size of the buffer being read is 1536 bytes so I don't see why a single call should take so long. At first I ignored this as a problem with my net connection, but this seemed to happen way too often and my net connection is never so unreliable. The strange thing is that the first call to InternetReadFile is very fast, but the second call always takes really long. Subsequent calls also don't have any problems. When I access the same URL using my browser, it comes down in under 10 secs where as the second loop of InternetReadFile causes a delay of 30+ seconds.
Does anyone know what the problem could be, or even an alrenate to InternetReadFile to read data.
Thanks,
- Sid
Re: InternetReadFile gets stuck in second loop
Please give us some code.
Mike
Re: InternetReadFile gets stuck in second loop
The code that i am using is pasted below. A lot of error checking has been removed, but that is the just of it.
Code:
m_hHTTPOpen = InternetOpen(_T("UserAgent"), INTERNET_OPEN_TYPE_PRECONFIG, "", "", 0) ;
if(!m_hHTTPOpen) e_xit ;
m_hHTTPConnection = InternetConnect( m_hHTTPOpen, strAddress, (INTERNET_PORT)dwPort, _T(""), _T(""),
INTERNET_SERVICE_HTTP,
INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_CACHE_WRITE, 0) ;
if(!m_hHTTPConnection) goto xit ;
if(InternetAttemptConnect(0) != ERROR_SUCCESS) goto xit ;
m_hHTTPRequest = HttpOpenRequest(m_hHTTPConnection, szMethod, strURI, HTTP_VERSION, "", 0,
INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_CACHE_WRITE, 0)
HttpSendRequest(m_hHTTPRequest, szContentType, sizeof(szContentType)-1, lpPostData, (DWORD)(nPostDataSize-1)) ;
while(InternetReadFile(m_hHTTPRequest, DataBuf, sizeof(DataBuf), &dwRead))
{ // Process the incoming data
}
- Sid
Re: InternetReadFile gets stuck in second loop
I think you need to give us more code. Your problem easily could be in the "process the incoming data" part, unless you have TRACE outputs that definitely rule this out (and tell us about those if you do).
Meanwhile, I have questions about this line:
Code:
HttpSendRequest(m_hHTTPRequest, szContentType, sizeof(szContentType)-1, lpPostData, (DWORD)(nPostDataSize-1)) ;
Why are you using sizeof()? Do you really want to send the entire buffer or just the "sz" part of it that might have meaningful data in it? Isn't strlen() correct? And why are you subtracting 1?
Same questions for lpPostData: Why are you sending the entire "Size" of the post data and why are you subtracting 1?
Maybe the line should read:
Code:
HttpSendRequest(m_hHTTPRequest, szContentType, strlen(szContentType), lpPostData, strlen(lpPostData) );
unless it's possible that your post data has some zero bytes in it, in which case you'll need to find another way to calculate the length of meaningful data.
Mike
Re: InternetReadFile gets stuck in second loop
I think you need to give us more code.
-> this is all the code for the HTTP connection. The rest of the code is just for building the buffers and input data. Anything specific that you are looking for?
Your problem easily could be in the "process the incoming data" part
-> I have stepped through the code using the debugger and the InternetReadFile is the one that takes the time, not the code inside the while block
Why are you using sizeof()?
-> szContentType is declared as
Code:
const CHAR szContentType[] = "Content-Type: application/x-www-form-urlencoded\r\n" ;
I don't see a problem with this.
The code specifically hangs at InternetReadFile. I've stepped through the code, put watches. I think I should go one step further and packet capture outgoing and incoming data. Lets see what comes up then.
- Sid
Re: InternetReadFile gets stuck in second loop
Hey
Did you find any reason for the function to get stuck?
Thanks,
Eyal Peleg