Link to home
Start Free TrialLog in
Avatar of visualc
visualc

asked on

Problem when a thread read from a socket...

HI!!
 I have a socket class. Here is the OnReceive function:

void CMySocket::OnReceive(int nErrorCode)
{
      CSocket::OnReceive(nErrorCode);
      WaitForSingleObject(m_hEventThreadOK, INFINITE);
      VERIFY(SetEvent(m_pThread->m_hEventRead));
}
 As you can see, the object wait until a thread object is initialized, then set a read event.
In the thread, I wait to the read event:
...
   WaitForSingleObject(m_hEventRead,INFINITE);
   try
   {
      pMsg->Serialize(*m_pArchiveIn);
   }
...
When I try to make a serialize trough a &m_pArchiveIn object, which is declared in CMySocket class, I get an assert in sockcore.cpp:
void CAsyncSocket::AssertValid() const
{
      CObject::AssertValid();
      ASSERT(m_hSocket == INVALID_SOCKET ||    CAsyncSocket::FromHandle(m_hSocket) != NULL);
}
But if I make the next:
void CMySocket::OnReceive(int nErrorCode)
{
      CSocket::OnReceive(nErrorCode);
      WaitForSingleObject(m_hEventThreadOK, INFINITE);
      m_pThread->ProcessPendingRead();
}
(I call direct the function from the thread object)-> its OK, but is not multithreaded!!!!!
What is the problem????(I want, that the thread to read from the socket, and process the data. There are some sockets objects, every has a "peer" thread, which must cooperate whit this...)
 Help!!
ASKER CERTIFIED SOLUTION
Avatar of plaroche
plaroche

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of visualc
visualc

ASKER

Hi!!
 I tried to get a CSocket object from a handle. In my thread I use now a constructor withh the next form:

CClientThread::CClientThread(CServerDoc* m_pDoc, SOCKET m_hSocket)
{
  this.m_hSocket = m_hSocket;
}
and in the InitInstance I try to get a pointer to my socket:

m_theSocket = CSocket::FromHandle(m_hSocket);

but the returned value is NULL!! (m_theSocket = NULL)

I tried to do this with Attach(m_hSocket), declaring a CMySocket
class in my thread (or a CSocket class). Doesn;t work with both.
The m_hSocket value is 33 (its OK?), and when the FromHandle function try to look up for this socket in his table, doesn't find this.
  Help!



Avatar of visualc

ASKER

All apologies!!
It's working!!! yesyesyes!@##@@!??>>#$!!!!!
Avatar of visualc

ASKER

All apologies!!
It's working!!! yesyesyes!@##@@!??>>#$!!!!!