Solved

Socket Notification Sink: How to rectify this problem

Posted on 2004-08-11
2
6,841 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

628 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