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

x
?
Solved

CSocket Threading

Posted on 1998-05-07
3
Medium Priority
?
622 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 180 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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 …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
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.
Suggested Courses
Course of the Month5 days, 19 hours left to enroll

773 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