1) The start of the prgram has the call to Internet Open call:
// Create Internet Handle
m_hInternet = InternetOpen(L"DataCenter", INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0);
if (m_hInternet == NULL)
{ ....}
2) The termination has the close of Internet Open handle:
if (m_hInternet)
{
InternetCloseHandle(m_hInternet);
m_hInternet = NULL;
}
3) the retrival of the file looks like this:
hInternetFile = InternetOpenUrl(m_hInternet, m_URL, NULL, 0,
INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_NO_UI | INTERNET_FLAG_PRAGMA_NOCACHE
| INTERNET_FLAG_RELOAD), 0);
if (hInternetFile == NULL)
{
AppendInternetErrorInfo(GetLastError(),&ErrorStr);
LogEvent(EVENTLOG_ERROR_TYPE, MSG_SERVICE_GEN_FAIL, L"\n\nInternet
handle failed to open. (Source: %s, %s", m_Name.m_str, ErrorStr.m_str);
m_bError = TRUE;
return;
}
dwChecksum = 0;
ulCurrentSize = 0;
while (InternetReadFile(hInternetFile, (LPVOID)szTemp, READ_BUFFER_SIZE,
&dwRead))
{
if (!dwRead) break;
// Add to size
ulCurrentSize += dwRead;
// Fill unused buffer with zero
if (dwRead < READ_BUFFER_SIZE)
memset(szTemp+dwRead, 0, READ_BUFFER_SIZE-dwRead);
// Calculate checksum
pData = (PDWORD)szTemp;
for (ix = 0; ix < iBuffersize; ix++)
dwChecksum ^= *pData++;
if (WriteFile(hFile, (LPVOID)szTemp, dwRead, &dwWritten, NULL) == 0)
{
LogEvent(EVENTLOG_ERROR_TYPE, MSG_SERVICE_GEN_FAIL, L"\n\nCannot
write to temporary file (Source: %s, Err:%d).\nFile: %s", m_Name.m_str,
GetLastError(), (wchar_t*)NewFilename);
InternetCloseHandle(hInternetFile);
hInternetFile = NULL;
CloseHandle(hFile);
hFile = NULL;
m_bError = TRUE;
return;
}
}
InternetCloseHandle(hInternetFile);
hInternetFile = NULL;
4) AppendInternetErrorInfo() definition
void CCoInternetSource::AppendInternetErrorInfo(DWORD dError, BSTR *ErrorStr)
{
TCHAR szTemp[356] = _T(""), *szBuffer=NULL;
DWORD dwIntError = 0, dwLength = 0;
_bstr_t Msg;
_stprintf(szTemp,_T(" Error:%d)\n"),dError);
Msg = szTemp;
FormatMessage(FORMAT_MESSAGE_FROM_HMODULE,
GetModuleHandle(L"wininet.dll"),dError,0,(LPTSTR)szTemp,256,NULL);
Msg += szTemp;
if (dError == ERROR_INTERNET_EXTENDED_ERROR)
{
InternetGetLastResponseInfo (&dwIntError, NULL, &dwLength);
if (dwLength)
{
if ( !(szBuffer = (TCHAR *) LocalAlloc ( LPTR, dwLength) ) )
return;
if (InternetGetLastResponseInfo (&dwIntError, (LPTSTR) szBuffer,
&dwLength))
{
Msg += L"\n";
Msg += szBuffer;
}
LocalFree (szBuffer);
}
}
SysReAllocString(ErrorStr, Msg);
}
What's happening is this:
- The file I am requesting does not exisit on the FTP server.
- The call to InternetOpenUrl returns null.
- InternetGetLastResponseInfo() returns a valid string saying unable to
retrieve file.
- After the program runs a while with attempt to retieve the file happening
every minute of so, I notice that netstat -n returns many sockets to that ftp
server in a CLOSE_WAIT State.
Frank.
Post by Paul Baker1. Are you calling InternetCloseHandle on the handle returned by
InternetOpen?
2. Are you calling InternetCloseHandle on the handled returned by
InternetOpenUrl if it succeeds?
3. Are you deallocating the buffer you allocated for
InternetGetLastResponseInfo?
4. Are you calling anything else?
Paul
Post by ffalconeI open an internet handle using InternetOpen().
Using that handle, I then use InternetOpenUrl() to access an FTP file. That
file does not exsist on the server, so I recieve a NULL handle back. When I
check the error using InternetGetLastResponseInfo(), I get the correct
response.
The problem I am seeing is that the process leaves the socket in a
CLOSE_WAIT state. After a while, I run out of socket resources. Is there any
information on a work around to this issue?
Thanks,
Frank