Discussion:
InternetOpenUrl causes sockets to stay in CLOSE_WAIT state
(too old to reply)
ffalcone
2005-11-30 21:29:02 UTC
Permalink
I 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
Paul Baker
2005-12-01 15:37:24 UTC
Permalink
1. 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 ffalcone
I 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
ffalcone
2005-12-01 16:16:11 UTC
Permalink
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 Baker
1. 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 ffalcone
I 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
Paul Baker
2005-12-01 16:39:51 UTC
Permalink
Is this running in a service? If so, this is just not supported.

INFO: WinInet Not Supported for Use in Services
http://support.microsoft.com/default.aspx?scid=kb;en-us;238425

As this article mentions, you should use WinHttp in this case.

Paul
Post by ffalcone
// Create Internet Handle
m_hInternet = InternetOpen(L"DataCenter", INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0);
if (m_hInternet == NULL)
{ ....}
if (m_hInternet)
{
InternetCloseHandle(m_hInternet);
m_hInternet = NULL;
}
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);
}
- 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 Baker
1. 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 ffalcone
I 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
ffalcone
2005-12-01 17:09:03 UTC
Permalink
Yes, it is in a dll that is used by a service. I will look into using
WinHttp, hopefully it has FTP support.

As a note, it seems to work fairly well in the service, except for this
problem I've run into after using it for about 2 years now.

Thanks, Frank
Post by Paul Baker
Is this running in a service? If so, this is just not supported.
INFO: WinInet Not Supported for Use in Services
http://support.microsoft.com/default.aspx?scid=kb;en-us;238425
As this article mentions, you should use WinHttp in this case.
Paul
Post by ffalcone
// Create Internet Handle
m_hInternet = InternetOpen(L"DataCenter", INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0);
if (m_hInternet == NULL)
{ ....}
if (m_hInternet)
{
InternetCloseHandle(m_hInternet);
m_hInternet = NULL;
}
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);
}
- 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 Baker
1. 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 ffalcone
I 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
Paul Baker
2005-12-01 18:27:47 UTC
Permalink
Well yeah, it will mostly work except that it won't ;)

I would get going with WinHttp if I were you.

Paul
Post by ffalcone
Yes, it is in a dll that is used by a service. I will look into using
WinHttp, hopefully it has FTP support.
As a note, it seems to work fairly well in the service, except for this
problem I've run into after using it for about 2 years now.
Thanks, Frank
Post by Paul Baker
Is this running in a service? If so, this is just not supported.
INFO: WinInet Not Supported for Use in Services
http://support.microsoft.com/default.aspx?scid=kb;en-us;238425
As this article mentions, you should use WinHttp in this case.
Paul
Post by ffalcone
// Create Internet Handle
m_hInternet = InternetOpen(L"DataCenter",
INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0);
if (m_hInternet == NULL)
{ ....}
if (m_hInternet)
{
InternetCloseHandle(m_hInternet);
m_hInternet = NULL;
}
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);
}
- 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 Baker
1. 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 ffalcone
I 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
ffalcone
2005-12-01 18:36:03 UTC
Permalink
Quick Question:

Turns out WinHTTP, which was suggested to be used, doesn't support FTP. Is
there an alternative to support FTP file gets in a service? Maybe a third
party solution?

Thanks again,
Frank.
Post by Paul Baker
Is this running in a service? If so, this is just not supported.
INFO: WinInet Not Supported for Use in Services
http://support.microsoft.com/default.aspx?scid=kb;en-us;238425
As this article mentions, you should use WinHttp in this case.
Paul
Post by ffalcone
// Create Internet Handle
m_hInternet = InternetOpen(L"DataCenter", INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0);
if (m_hInternet == NULL)
{ ....}
if (m_hInternet)
{
InternetCloseHandle(m_hInternet);
m_hInternet = NULL;
}
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);
}
- 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 Baker
1. 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 ffalcone
I 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
Paul Baker
2005-12-01 20:28:55 UTC
Permalink
Good point. Yes, you'll need a third party solution. I am not in a position
to recommend any, as I use WinInet ;)

Paul
Post by ffalcone
Turns out WinHTTP, which was suggested to be used, doesn't support FTP. Is
there an alternative to support FTP file gets in a service? Maybe a third
party solution?
Thanks again,
Frank.
Post by Paul Baker
Is this running in a service? If so, this is just not supported.
INFO: WinInet Not Supported for Use in Services
http://support.microsoft.com/default.aspx?scid=kb;en-us;238425
As this article mentions, you should use WinHttp in this case.
Paul
Post by ffalcone
// Create Internet Handle
m_hInternet = InternetOpen(L"DataCenter",
INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0);
if (m_hInternet == NULL)
{ ....}
if (m_hInternet)
{
InternetCloseHandle(m_hInternet);
m_hInternet = NULL;
}
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);
}
- 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 Baker
1. 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 ffalcone
I 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
Loading...