Solved

CSocket Threading

Posted on 1998-05-07
3
597 Views
Last Modified: 2013-11-20
I've got the follow bit of code for a multi-threaded
socket server:

(CClientSocket is derived from CSocket)
------[CServerDlg.cpp]----------

// This function is called when a read request comes in

void CServerDlg::OnSocketRead(CClientSocket *pCSocket)
{
AfxBeginThread(Read,pCSocket,THREAD_PRIORITY_LOWEST);
}

// This is the thread the above function calls

UINT Read(LPVOID pParam)
{
CClientSocket *pCSocket=(CClientSocket *)pParam;

CString str;
pCSocket->m_pArchiveIn->ReadString(str);
// IT ASSERTS ON THE CALL ABOVE
...

----------------------------------------

It asserts here if the program is compiled in Debug configuration. If I compile in Release it skips the
checks in sockcore and this part of program works
just fine.

---------[Sockcore.cpp: Line 755]----------

void CAsyncSocket::AssertValid() const
{
CObject::AssertValid();
ASSERT(m_hSocket == INVALID_SOCKET || CAsyncSocket::FromHandle(m_hSocket)
!= NULL);      // IT FAILS HERE: LINE 755
}

------------------------------------------------------

When debug runs it shows m_hSocket having a value of about 400 and INVALID_SOCKET should be -1 or NULL.. I don't understand why it's asserting.

any ideas? or just a good example of a multi-threaded
socket server for mfc will do. CHATSVR is single threaded
and wont do multiuser file transfers well...
0
Comment
Question by:forloop
3 Comments
 
LVL 1

Accepted Solution

by:
Blondie050798 earned 60 total points
Comment Utility
You have to be careful about passing C++ objects between threads...especially MFC ones where lots of mapping is carried out...I suspect that the ASSERT is due to FromHandle() not INVALID_SOCKET.  Is your worker function a friend of the class?  This is the best way to then access anything from that class...
0
 

Expert Comment

by:footloose
Comment Utility
The MFC documentation says you cannot access MFC objects between threads. You will have
to handle the CClientSocket in whatever window you originally created. Somebody asked a similar question earlier (and I posted a similar comment  :-). Look in the PAQ's.
0
 

Author Comment

by:forloop
Comment Utility
Well, I found some great source elsewhere on the net that has a complete MT socket server. It looks well written and should provide a good base to add things to.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Doc'in system (example?) BA 7 78
twoTwo  challenge 35 85
sumHeights  challenge 17 59
wordmultiple challenge 12 87
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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 shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

6 Experts available now in Live!

Get 1:1 Help Now