Solved

Problem when a thread read from a socket...

Posted on 1998-11-30
4
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

 Database Backup and Recovery Best Practices

Join Percona’s, Architect, Manjot Singh as he presents Database Backup and Recovery Best Practices (with a Focus on MySQL) on Thursday, July 27, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7). In the case of a failure, do you know how long it will take to restore your database?

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

623 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