Solved

Socket Notification Sink: How to rectify this problem

Posted on 2004-08-11
2
6,820 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

778 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