Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Where should I put CSocket code in MFC dialog app?

Status
Not open for further replies.

mrutherford

Programmer
Sep 5, 2003
6
0
0
GB
Hi, i'm a convert from Java and am weak at general MFC howto. Sample apps, such as those on MSDN & codeproject, make sense to me in isolation when they deal with a particular subject, but im trying to figure the following out:

As part of a simple Client/Server app, my server is built using the MFC app wizard. It is a simple Dialog based app(not SDI nor MDI). Code is divided chiefly into 2 classes: the CxxxServerApp and CxxxServerDlg.

I want to set it up so that it listens to incoming requests immediately, and ideally, passes them off to separate threads. Should I create another container class for this work (specifically the thread stuff) and where in the app should (or can) I set up the CSocket member functions listen() and accept()?

any help greatly appreciated.
 
objects what are up to application execution such as sockets usualy are memebrs of CxxxYyyApp

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
To keep the UI active you will need to accept() in a worker thread. The resulting socket handle from the accept() will need to be passed to worker threads for processing (session) if you want the accept() thread to be able to continue to accept() more connections.

For you server to scale you will need to implement a thread pool, or something else (fibers, processes etc.), for the sessions. If you don’t need to scale perhaps not.

Do not pass MFC object pointers across threads. MFC uses TLS and this will break. There are several articles on MSDN regarding that issue.

Let me know if you need more specific information. [cheers]


-pete
 
thanks palbano,
from experience, which Thread classes would be easiest to implement: the managed variety in System::Threading namespace or what is the equivalent in MFC, if supported?

also am i right in thinking I can utilise the .NET lib simply by using the appropriate namespace at the start of my code?
 
To use any .NET library classes you need to do a Managed Project. I don’t know if or how you can change an existing project to managed ( I would assume you can).

I would not recommend a managed project though unless you have specific requirements for one, and I don’t have any idea what they would be. My limited knowledge would lead me to believe you use the power of C++ to build .NET library components that C# and VB.NET languages can consume. Not to use .NET from a C++ application. That seems a little like putting a 25 horse outboard on a 50 foot yacht.

For starting your worker threads I would use _beginthread() or CreateThread() family of functions. If your going to use MFC classes in the worker threads you want to use AfxBeginThread().

Keep those questions coming [2thumbsup]


-pete
 
thanks alot pete, sorry for the wide spread of questions there, im still at the stage where I still dont know the right questions, never mind the right answers!

cheers
 
>> Keep those questions coming

Just in case you thought I was joking, NOT. Socket servers and clients and threading are one of my favorite subjects. [cheers]


-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top