Solved

CSocket Threading

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
substring method in java 1 97
either24  challenge 19 90
Window placement 17 74
Remove Password Protection from pdf (with password) using an command line executable 5 22
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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

914 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

18 Experts available now in Live!

Get 1:1 Help Now