Solved

Problem when a thread read from a socket...

Posted on 1998-11-30
4
246 Views
Last Modified: 2013-11-20
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!!
0
Comment
Question by:visualc
  • 3
4 Comments
 
LVL 3

Accepted Solution

by:
plaroche earned 100 total points
ID: 1325516
You CANNOT use MFC objects across threads.

Use the handle in CSocket to pass to the thread, then in the thread do a CSocket::FromHandle();

Now you will have a CSocket object you can use in that thread.
0
 

Author Comment

by:visualc
ID: 1325517
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!



0
 

Author Comment

by:visualc
ID: 1325518
All apologies!!
It's working!!! yesyesyes!@##@@!??>>#$!!!!!
0
 

Author Comment

by:visualc
ID: 1325519
All apologies!!
It's working!!! yesyesyes!@##@@!??>>#$!!!!!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question