Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem when a thread read from a socket...

Posted on 1998-11-30
4
Medium Priority
?
254 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 300 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

916 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