Solved

CSocket Threading

Posted on 1998-05-07
3
616 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
[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 Comments
 
LVL 1

Accepted Solution

by:
Blondie050798 earned 60 total points
ID: 1308370
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
ID: 1308371
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
ID: 1308372
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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. …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

615 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