Discussion:
Help with wininet and threads
(too old to reply)
m***@gmail.com
2005-03-07 18:06:50 UTC
Permalink
Hi all,

My question is about wininet and multithreading. It is not a general
question but a specific one.

I made research on the net but did not find any answers or solutions.
If this is a well known problem with posted solutions my apologies. So,
here is the problem:

Programming language: C++
Compiler/linker : VC .Net
OS : Windows XP latest Service Pack

Objective is to grab images from several IP Cameras using HTTP API
specs. of the cameras by polling. Basically these are simple HTTP
requests.

I encapsulate the HTTP communication routines in a class. This class
also has a function entry point for multithreading: a static class
function which is passed to beginthread(), calling a regular class
method where all necessary work is done.

For each camera there is an instance of this class and of course a
separate thread. The thread simply uses InternetOpenUrl() to make the
HTTP request (asking for image data) which then is read using
InternetReadFile(). I believe I am also making necessary error
checkings in between. The calls to InternetOpenUrl() and
InternetReadFile() are in a loop which terminates when a flag is set to
terminate the thread. So far so good.

The weird thing is, when more than one thread (or cameras) are working,
after some time (1-2 minutes), I start getting 12029 errors. My code
handles these errors by just skipping that connection attempt but the
error frequency becomes very high and at the end makes the process
unstable. When there is only one thread running there are no problems.
It seems to me like some kind of buffer is filled after sometime and
then connection attempts start to fail. Or, it may also be a
multithreading synchronization problem.

Do you have any idea ? Is there anyone else having similar problems ?
Do you think I should switch to winhttp (which I really don't to want
due to all Unicode conversions I have to make) ?

Thank you for your help,

Murat
Paul Baker
2005-03-07 19:25:34 UTC
Permalink
Hi,

12029 is ERROR_INTERNET_CANNOT_CONNECT. This would suggest that the HTTP
server is refusing connections.

It is possible that you are failing to close the connection when you are
done. These connections could then be building up over time nad reaching the
limit of the HTTP server.

When the error message occurs, you could try connecting to the HTTP server
using other methods, and see if is really just refusing connections. You
could use a browse or you could use telnet on port 80.

I don't think threads is the issue. WinInet is thread-safe.

Paul
Post by m***@gmail.com
Hi all,
My question is about wininet and multithreading. It is not a general
question but a specific one.
I made research on the net but did not find any answers or solutions.
If this is a well known problem with posted solutions my apologies. So,
Programming language: C++
Compiler/linker : VC .Net
OS : Windows XP latest Service Pack
Objective is to grab images from several IP Cameras using HTTP API
specs. of the cameras by polling. Basically these are simple HTTP
requests.
I encapsulate the HTTP communication routines in a class. This class
also has a function entry point for multithreading: a static class
function which is passed to beginthread(), calling a regular class
method where all necessary work is done.
For each camera there is an instance of this class and of course a
separate thread. The thread simply uses InternetOpenUrl() to make the
HTTP request (asking for image data) which then is read using
InternetReadFile(). I believe I am also making necessary error
checkings in between. The calls to InternetOpenUrl() and
InternetReadFile() are in a loop which terminates when a flag is set to
terminate the thread. So far so good.
The weird thing is, when more than one thread (or cameras) are working,
after some time (1-2 minutes), I start getting 12029 errors. My code
handles these errors by just skipping that connection attempt but the
error frequency becomes very high and at the end makes the process
unstable. When there is only one thread running there are no problems.
It seems to me like some kind of buffer is filled after sometime and
then connection attempts start to fail. Or, it may also be a
multithreading synchronization problem.
Do you have any idea ? Is there anyone else having similar problems ?
Do you think I should switch to winhttp (which I really don't to want
due to all Unicode conversions I have to make) ?
Thank you for your help,
Murat
ismailp
2005-03-08 22:05:28 UTC
Permalink
ok, i am not sure whether or not i understood that thread creation
part, checkout this out:

class CHttpCam
{
HINTERNET m_hInet;
HINTERNET m_hCon;
HANDLE m_hThread;
DWORD _ThreadProc()
{
m_hInet = InternetOpen();
m_hCon = InternetConnect();
// do rest of work here
return 0;
}
public:
CHttpCam()
{
m_hThread = NULL;
}
~CHttpCam()
{
if (m_hThread)
CloseHandle(m_hthread);
}
static DWORD CALLBACK ThreadEntrypoint(LPVOID lpv)
{
return ((CHttpCam*)lpv)->_ThreadProc();
}
BOOL Start()
{
DWORD dwTid;

// you can use QueueUserWorkItem as well.
return ((m_hThread = CreateThread(NULL, 0,
CHttpCam::ThreadEntrypoint, (LPVOID)this, 0, &dwTid)) == NULL);
}
};

So, for each camera, you can call Start(). Hope that helps.

Ismail
TxITGuy
2005-03-14 22:59:05 UTC
Permalink
Don't forget to call:

InternetCloseHandle(m_hInet);
InternetCloseHandle(m_hCon);

at the end of _ThreadProc() ... otherwise you'll eventually run out of
handles.

Ben
Post by ismailp
ok, i am not sure whether or not i understood that thread creation
class CHttpCam
{
HINTERNET m_hInet;
HINTERNET m_hCon;
HANDLE m_hThread;
DWORD _ThreadProc()
{
m_hInet = InternetOpen();
m_hCon = InternetConnect();
// do rest of work here
return 0;
}
CHttpCam()
{
m_hThread = NULL;
}
~CHttpCam()
{
if (m_hThread)
CloseHandle(m_hthread);
}
static DWORD CALLBACK ThreadEntrypoint(LPVOID lpv)
{
return ((CHttpCam*)lpv)->_ThreadProc();
}
BOOL Start()
{
DWORD dwTid;
// you can use QueueUserWorkItem as well.
return ((m_hThread = CreateThread(NULL, 0,
CHttpCam::ThreadEntrypoint, (LPVOID)this, 0, &dwTid)) == NULL);
}
};
So, for each camera, you can call Start(). Hope that helps.
Ismail
Continue reading on narkive:
Loading...