Solved

Problem when a thread read from a socket...

Posted on 1998-11-30
4
244 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Handling string inputs in C/Linux 23 167
ODBC Connection Logging, ADO.NET 6 49
move a line in eclipse 3 60
matchUp  challenge 9 71
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 …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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 video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now