Solved

Using CAsyncSocket from multiple threads

Posted on 2000-02-22
1
739 Views
Last Modified: 2013-11-20
I am trying to use CAsyncSocket to create two separate sockets in two separate threads. One is being created in the main application thread, the other in a worker thread. I have no problems creating the socket in the main thread, but when I try to call Create() in the worker thread, the call dies inside CAsyncSocket. I traced through the MFC code and it seems there are some thread local data structures which store the socket handles used by CAsyncSocket. These structures appear not to have been intialized for my worker thread as the pointers to them are NULL. After some further searching I found that these data structures are initialized by AfxSocketInit(). I found that if I call AfxSocketInit() from within my worker thread, I can create sockets within it. This only workes if I remove the AfxSocketInit() from my main thread as MFC doesn't let you Initialize sockets twice. This prevents me from being able to create sockets in the main thread.

I find it hard to believe that there is no way to create CAsyncSockets in multiple threads and I have seen nothing in the documentation that would indicate that this is the case. Therefore I assume I must be doing something wrong. Is there anything I need to do to prepare a thread to create a socket? Would it help if I used a user interface thread instead of a worker thread? Is there some sort of limitation on using CAsyncSocket with multiple threads? Any help would be appreciated....
0
Comment
Question by:jhanson040697
1 Comment
 

Accepted Solution

by:
JohnnyXia earned 150 total points
ID: 2549156
jhanson,

In order to achieve ansynchronous operation of socket, MFC will create a window for a socket as a sink object to receive Socket Messages. So that in order to use Socket class of MFC, you must have a message loop for the thread which the Socket Class is created in.

To resolve the problem you have 2 choice:

1)Add a message loop to your work thread.

2)Use the socket API directly.( I preferred to this one. For CAnsyncSocket is designed to use in a single thread especially.)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 47
Powershell based monitoring system 2 127
GIF file bit manipulation for color change 5 104
ORA-01403: no data found 43 82
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…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

15 Experts available now in Live!

Get 1:1 Help Now