Re: Joe's AsyncSocket Example
Well... here is the problem I have, that I assume is related to
multi-threaded programs
I have placed all client socket code into am MFC Extension DLL. The
application links to the DLL and in the View class, I have two pointers to
CClientThreads. I have elected to use OnTimer to initate my connections
because if the connections drop, I reinstall the timer to attempt
reconnection until connection succeeds, at which point I kill the timer.
that said, the openthread function is as follows.
void CERAMRSideView::OpenThreads()
{
KillTimer(TRY_CONNECT);
CERAMRSideDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
TRACE(_TEXT(" CERAMRSideView::OpenThreads()\n") );
if( !m_simConnected && !m_simConnecting)
{
// Create a thread to handle the connection. The thread is created
suspended so that we can
// set variables in CClientThread before it starts executing.
m_pSimThread =
(CClientThread*)AfxBeginThread(RUNTIME_CLASS(CClientThread),
THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
if (m_pSimThread == NULL)
{ /* failed to start */
CString fmt;
fmt.LoadString(IDS_THREAD_CREATE_FAILED);
CString s;
s.Format(fmt, pDoc->m_simServer);
TRACE(_T("%s: %s\n"), AfxGetApp()->m_pszAppName, s);
return;
} /* failed to start */
TRACE(_T(" CERAMRSideView::OpenThreads() Opened Sim Socket.\n") );
// set the thread up
m_pSimThread->SetTarget(this);
m_pSimThread->SetServerName(pDoc->m_simServer);
m_pSimThread->SetPort(pDoc->m_simPort);
// Now start the thread.
m_pSimThread->ResumeThread();
}
if( !m_nasConnected && !m_nasConnecting)
{
// Create a thread to handle the connection. The thread is created
suspended so that we can
// set variables in CClientThread before it starts executing.
m_pNasThread =
(CClientThread*)AfxBeginThread(RUNTIME_CLASS(CClientThread),
THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
if (m_pNasThread == NULL)
{ /* failed to start */
CString fmt;
fmt.LoadString(IDS_THREAD_CREATE_FAILED);
CString s;
s.Format(fmt, pDoc->m_nasServer);
TRACE(_T("%s: %s\n"), AfxGetApp()->m_pszAppName, s);
return;
} /* failed to start */
TRACE(_T(" CERAMRSideView::OpenThreads() Opened Nas Socket.\n") );
// set the thread up
m_pNasThread->SetTarget(this);
m_pNasThread->SetServerName(pDoc->m_nasServer);
m_pNasThread->SetPort(pDoc->m_nasPort);
// Now start the thread.
m_pNasThread->ResumeThread();
}
}
the problem is this. A call to AfxRegisterClass() that happens inside of
the SocketAttach() call of the socket fails and throws a CResourceException.
this does not happen everytime. Sometimes it opens up just fine, connected
to the two different servers I have and all is fine. And sometimes the app
throws this exception. Additionally, the application sometimes hangs my
entire computer to the point of mouse clicks taking 60 seconds to occur
Possibly, instead of starting both threads in the same function, I should do
it in seperate functions and signal between them? I am not sure what to do
because I am no expert at threading. I assume threading is at the core of
the problem because this error only occurs sometimes.
.... after looking at it further just now, inside of the
CClientThread::InitInstance() the call to m_socket.Create() is what throws
the CResourceException. Unfortunately, the exception object really has no
decription in it at all... but I do notice that both calls that create the
threads in the above OpenSockets have happened because the thread IDs exist
at the point the error happens. So in the code I have, m_socket.Create() is
the culprit, but in digging into the MFC code, the culprit is a call to
::AfxCtxRegisterClass() and in debug the failure of this call ASSERTs with
"Can't register window class named Afx:00400000:0". That call happens
inside of AfxRegisterClass(), FYI.
Is this enough information to have an idea as to the problem?
"Joseph M. Newcomer" <newcomer@flounder.com> wrote in message
news:5b4iu310oit86ur5nubf45j8s4nj4fac0r@4ax.com...
No, it should be completely symmetric. My example application only
connects to one
server, but that's only because I wanted a simple GUI interface. But as
far as I know,
there's nothing in the code that limits a client in any way.
joe
On Tue, 25 Mar 2008 08:28:34 -0500, "asellon" <patriot92@cox.net> wrote:
Joe,
I have a need to connect a single application to 2 servers. So, I need
a
client application that has 2 client sockets. Do you foresee any issues
with using the current clientsocket from your example in this manner? The
plan is to use a little of your server style code and maintain a
DWORDArray
of the thread ids(which will be two of them) and then use the functions
pretty much as is.
I guess the whole point is in the case of using the client socket code,
what
needs to be considered to run two client socket threads in the same
client?
Anything special to consider?
thanks
Andy
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm