Solved

Socket Notification Sink: How to rectify this problem

Posted on 2004-08-11
2
6,800 Views
Last Modified: 2013-11-20
Hello all,

My previous team has used MFC Sockets to create an application which is used as both server-client. ForMy Current application we are using  the above application. But Some times we are facing Socket Notification Sink problem from the previous application

My Current Application uses BSD sockets whereas previous one uses MFC Sockets.

Some where I read on internet, that it is not acceptable to use MFC Sockets as it has problems while working in multi threaded environment.  

Now it is not possible to rewrite entire previous application.

So I would like to know how to resove this problem using MFC Sockets itself.

Thanks and Best Regards
Sudhakar
0
Comment
Question by:sudhakar_koundinya
2 Comments
 
LVL 49

Accepted Solution

by:
DanRollins earned 250 total points
ID: 11786262
MFC sockets work great in multi-threading, as long as each socket is created by the thread that uses it (or is passed by handle correctly).

If you do not have access to the older program's source code to track down and eliminate bugs, I think you will be out of luck.  If it is a *very* old program, there is a small chance that ensuring you are using the best version of mfc42.dll will help matters, but that DLL has been stable for many years, so that is probably not the problem.

-- Dan
0
 
LVL 3

Assisted Solution

by:pulupul
pulupul earned 250 total points
ID: 11787326
I have used the same MFC socket (CAsyncSocket or CSocket, doesn't matter) in various threads without a problem. The only tricky thing is that you cant use the same MFC socket object directly in two threads. You have to call CAsyncSocket::Detach() on the thread that created the socket, and it will return a winsock SOCKET object. Now you can pass this SOCKET object to any other thread. In the other thread, you must create a CAsyncSocket, and call it's member function Attach(), passing the SOCKET object as the argument. At this point you can continue using the socket normally.
Finally, remember to call AfxSocketInit() in each thread you use the MFC sockets subystem, otherwise it won't work.
Some code examples:

- As inter process communication is beyond the scope of this aswer, I will use a global variable to pass the socket between threads:

SOCKET g_WinSocket;

- In the thread that creates the socket:

AfxSocketInit();
CAsyncSocket mfcsocket;
if (!mfcsocket.Create() || !mfcsocket.Connect("192.168.0.1",80)
   return;
mfcsocket.Send(buffer,sizeof(buffer));
g_WinSocket = mfcsocket.Detach();

- In the other thread:

AfxSocketInit();
CAsyncSocket mfcsocket;
mfcsocket.Attach(g_WinSocket);
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Detect CR LF to each line 12 137
fizzArray2 challenge 1 61
no14 challenge 14 57
Execute multiple curl cmds with sleep and send output to file 10 80
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

11 Experts available now in Live!

Get 1:1 Help Now