Solved

Socket Notification Sink: How to rectify this problem

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 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

18 Experts available now in Live!

Get 1:1 Help Now