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
Solved

Socket Notification Sink: How to rectify this problem

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wait notify demo infinite loop 3 124
pre4 challenge 19 104
scoreUp challenge 14 64
how to split multiple lines delimiter : 8 111
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

829 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