Solved

CSocket Threading

Posted on 1998-05-07
3
606 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
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
dog bark java program 15 95
fizzArray2 challenge 1 80
ASP.NET C# MessageBox.Show Showing a modal dialog box or form when the application ... 2 128
Detect file exist or not 3 158
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 …
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

809 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