Discussion:
can you help me identify what is wrong with my program.
(too old to reply)
Jakein2006
2006-12-05 09:09:31 UTC
Permalink
I want to use the following code to detect internet connection:

but when the program run into the workfunction, it return 1, so when the
internet connection is good, it return false, can you help me identify what
is wrong with my program.

I appreciate your help.


BOOL
bOnline=m_VirtualEarth.OnLine(TEXT("dev.virtualearth.net/mapcontrol/v3/mapcontrol.js"));

HINTERNET g_hRequest = NULL;

BOOL CVirtualEarth::OnLine(CString url)
{
int index,nLen;
CString strHost = url;
CString strObject = "";
nLen = url.GetLength();
index = url.Find(TEXT("//"));
if (index != -1)
{
strHost = url.Left(index);
if (nLen>index)
{
strObject = url.Right(nLen-index);
}
}

HINTERNET hInetSession = NULL;
HINTERNET hHttpConnection = NULL;
HINTERNET hRequest = NULL;

if(!(hInetSession = InternetOpen(TEXT("HtmlStream"),
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
0)))
{
AfxMessageBox(_T("InternetOpen failed"));
return FALSE;
}


DWORD flag = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE |
INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_KEEP_CONNECTION;

hHttpConnection = InternetConnect(hInetSession, strHost,
INTERNET_DEFAULT_HTTP_PORT,
NULL,
NULL,
INTERNET_SERVICE_HTTP,
0,
0);

g_hRequest = HttpOpenRequest(hHttpConnection, TEXT("GET"), strObject,
TEXT("HTTP/1.0"), NULL, NULL, flag, 0);
if(g_hRequest == NULL)
{
InternetCloseHandle(hHttpConnection);
InternetCloseHandle(hInetSession);
return FALSE;
}

DWORD dwTimeout;
DWORD m_uThreadId;
HANDLE hThread = CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)CVirtualEarth::WorkFunction,
this,
0,
&m_uThreadId);
dwTimeout = 25000;
if ( WaitForSingleObject ( hThread, dwTimeout ) == WAIT_TIMEOUT )
{
CString str;
str.Format(TEXT("Can not connect to http server in %d
milliseconds"),dwTimeout);
AfxMessageBox(str);

if ( hInetSession )
InternetCloseHandle ( hInetSession );
// Wait until the worker thread exits
WaitForSingleObject ( hThread, INFINITE );
return FALSE;
}

DWORD dwExitCode = 0;
if ( !GetExitCodeThread( hThread, &dwExitCode ) )
{
return FALSE;
}
CloseHandle (hThread);

if ( dwExitCode )
// Worker function failed
return FALSE;

if (hRequest)
{
InternetCloseHandle(hRequest);
}

if (hHttpConnection)
{
InternetCloseHandle(hHttpConnection);
}

if (hInetSession)
{
InternetCloseHandle(hInetSession);
}

return TRUE;

}

DWORD CVirtualEarth::WorkFunction(LPVOID lpParam)
{
BOOL b = HttpSendRequest(g_hRequest, NULL, 0, NULL, 0);
if (!b)
{
return 1;
}
return 0;


}
--
jake
Vladimir Scherbina
2006-12-05 11:41:12 UTC
Permalink
There are several problems with this code:

1. There is no need to make so complicated design and devide wininet
initialization code and wininet communicatioin code into two different
threads.
2. If HttpSend will hung - for example, because of low connection your code
will treat it as "no connection".
3. You waste a lot of resources by creating additional thread and calling
WaitXxx functions - transition to kernel mode is expensive.
4. It's not a good idea to detect site avialibility by connecting to it
using HTTP protocol.
--
Vladimir (Windows SDK MVP)
http://msmvps.com/blogs/v_scherbina/
Post by Jakein2006
but when the program run into the workfunction, it return 1, so when the
internet connection is good, it return false, can you help me identify what
is wrong with my program.
I appreciate your help.
BOOL
bOnline=m_VirtualEarth.OnLine(TEXT("dev.virtualearth.net/mapcontrol/v3/mapcontrol.js"));
HINTERNET g_hRequest = NULL;
BOOL CVirtualEarth::OnLine(CString url)
{
int index,nLen;
CString strHost = url;
CString strObject = "";
nLen = url.GetLength();
index = url.Find(TEXT("//"));
if (index != -1)
{
strHost = url.Left(index);
if (nLen>index)
{
strObject = url.Right(nLen-index);
}
}
HINTERNET hInetSession = NULL;
HINTERNET hHttpConnection = NULL;
HINTERNET hRequest = NULL;
if(!(hInetSession = InternetOpen(TEXT("HtmlStream"),
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
0)))
{
AfxMessageBox(_T("InternetOpen failed"));
return FALSE;
}
DWORD flag = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE |
INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_KEEP_CONNECTION;
hHttpConnection = InternetConnect(hInetSession, strHost,
INTERNET_DEFAULT_HTTP_PORT,
NULL,
NULL,
INTERNET_SERVICE_HTTP,
0,
0);
g_hRequest = HttpOpenRequest(hHttpConnection, TEXT("GET"), strObject,
TEXT("HTTP/1.0"), NULL, NULL, flag, 0);
if(g_hRequest == NULL)
{
InternetCloseHandle(hHttpConnection);
InternetCloseHandle(hInetSession);
return FALSE;
}
DWORD dwTimeout;
DWORD m_uThreadId;
HANDLE hThread = CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)CVirtualEarth::WorkFunction,
this,
0,
&m_uThreadId);
dwTimeout = 25000;
if ( WaitForSingleObject ( hThread, dwTimeout ) == WAIT_TIMEOUT )
{
CString str;
str.Format(TEXT("Can not connect to http server in %d
milliseconds"),dwTimeout);
AfxMessageBox(str);
if ( hInetSession )
InternetCloseHandle ( hInetSession );
// Wait until the worker thread exits
WaitForSingleObject ( hThread, INFINITE );
return FALSE;
}
DWORD dwExitCode = 0;
if ( !GetExitCodeThread( hThread, &dwExitCode ) )
{
return FALSE;
}
CloseHandle (hThread);
if ( dwExitCode )
// Worker function failed
return FALSE;
if (hRequest)
{
InternetCloseHandle(hRequest);
}
if (hHttpConnection)
{
InternetCloseHandle(hHttpConnection);
}
if (hInetSession)
{
InternetCloseHandle(hInetSession);
}
return TRUE;
}
DWORD CVirtualEarth::WorkFunction(LPVOID lpParam)
{
BOOL b = HttpSendRequest(g_hRequest, NULL, 0, NULL, 0);
if (!b)
{
return 1;
}
return 0;
}
--
jake
Vladimir Scherbina
2006-12-05 12:34:46 UTC
Permalink
It depends upon the requirements. If you need to know that host is online,
then you can just ping it.
--
Vladimir (Windows SDK MVP)
http://msmvps.com/blogs/v_scherbina/
Hi, thanks for your answer,
Can you tell me what kind of protocol should I use to detect the website
availibility?
I appreciate your help.
--
jake
Post by Vladimir Scherbina
1. There is no need to make so complicated design and devide wininet
initialization code and wininet communicatioin code into two different
threads.
2. If HttpSend will hung - for example, because of low connection your code
will treat it as "no connection".
3. You waste a lot of resources by creating additional thread and calling
WaitXxx functions - transition to kernel mode is expensive.
4. It's not a good idea to detect site avialibility by connecting to it
using HTTP protocol.
--
Vladimir (Windows SDK MVP)
http://msmvps.com/blogs/v_scherbina/
Post by Jakein2006
but when the program run into the workfunction, it return 1, so when the
internet connection is good, it return false, can you help me identify what
is wrong with my program.
I appreciate your help.
BOOL
bOnline=m_VirtualEarth.OnLine(TEXT("dev.virtualearth.net/mapcontrol/v3/mapcontrol.js"));
HINTERNET g_hRequest = NULL;
BOOL CVirtualEarth::OnLine(CString url)
{
int index,nLen;
CString strHost = url;
CString strObject = "";
nLen = url.GetLength();
index = url.Find(TEXT("//"));
if (index != -1)
{
strHost = url.Left(index);
if (nLen>index)
{
strObject = url.Right(nLen-index);
}
}
HINTERNET hInetSession = NULL;
HINTERNET hHttpConnection = NULL;
HINTERNET hRequest = NULL;
if(!(hInetSession = InternetOpen(TEXT("HtmlStream"),
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
0)))
{
AfxMessageBox(_T("InternetOpen failed"));
return FALSE;
}
DWORD flag = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE |
INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_KEEP_CONNECTION;
hHttpConnection = InternetConnect(hInetSession, strHost,
INTERNET_DEFAULT_HTTP_PORT,
NULL,
NULL,
INTERNET_SERVICE_HTTP,
0,
0);
g_hRequest = HttpOpenRequest(hHttpConnection, TEXT("GET"), strObject,
TEXT("HTTP/1.0"), NULL, NULL, flag, 0);
if(g_hRequest == NULL)
{
InternetCloseHandle(hHttpConnection);
InternetCloseHandle(hInetSession);
return FALSE;
}
DWORD dwTimeout;
DWORD m_uThreadId;
HANDLE hThread = CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)CVirtualEarth::WorkFunction,
this,
0,
&m_uThreadId);
dwTimeout = 25000;
if ( WaitForSingleObject ( hThread, dwTimeout ) == WAIT_TIMEOUT )
{
CString str;
str.Format(TEXT("Can not connect to http server in %d
milliseconds"),dwTimeout);
AfxMessageBox(str);
if ( hInetSession )
InternetCloseHandle ( hInetSession );
// Wait until the worker thread exits
WaitForSingleObject ( hThread, INFINITE );
return FALSE;
}
DWORD dwExitCode = 0;
if ( !GetExitCodeThread( hThread, &dwExitCode ) )
{
return FALSE;
}
CloseHandle (hThread);
if ( dwExitCode )
// Worker function failed
return FALSE;
if (hRequest)
{
InternetCloseHandle(hRequest);
}
if (hHttpConnection)
{
InternetCloseHandle(hHttpConnection);
}
if (hInetSession)
{
InternetCloseHandle(hInetSession);
}
return TRUE;
}
DWORD CVirtualEarth::WorkFunction(LPVOID lpParam)
{
BOOL b = HttpSendRequest(g_hRequest, NULL, 0, NULL, 0);
if (!b)
{
return 1;
}
return 0;
}
--
jake
Loading...