CAsyncSocket in UI thread (Mr. Newcomer)

CAsyncSocket in UI thread (Mr. Newcomer)

Post by Pabl » Sun, 07 Dec 2003 07:49:31


r Newcomer.

Previously, in a newsgroup post you had responded to another thread where I
had asked about some operations with CAsyncSocket and you noted that I
couldn't use CAsyncSocket in a worker thread- then you directed me to your
web page to read your article about CAsyncSocket in UI threads. I wanted to
send you a snippet of code (I've reduced it as much as possible to leave
only the relevant stuff). Also, I apologize in advance for specifying you
on the subject line. Previously, I had tried sending you an email from the
link on your website but all my attempts were bounced back as 'not accepting
email from spammers'. I tried under several email addresses (mine) both
well known and super-secret, all got bounced as 'spam'. Oh well.

Here is a section of server code which I have running in a worker thread
started with AfxBeginThread([func],this);

Also, forgive the sloppy coding, this is something I wrote as a prototype to
understand functionality- plus email formatting has broken some of the
neatness.

Anyway, this code works and I currently have it running on my desktop now.
I'm curious as to how this code works if one should not be able to get it
working. My assumption at this point is to defer to your assertion that
CAsyncSocket does not run properly in a worker thread, and I've got some
process that is causing my code to unwittingly work.
My structure is basically as thus: A single 'listener' thread starts,
creating a listener socket which, after a successful connection is made (see
the heavily commented block near end of Listener() member function)
it detaches and launches a new thread in a 'talker thread'. As you may
guess, there is a unique thread for each connection established, but only
one thread which listens for and accepts connections. After the 'listener'
function below, see the 'talker' function . Feel free to pester me for any
further clarifications. Thank you in advance for any time and consideration
you give to my email.

bool CRedEyeDlg::Listener()
{
m_bThreadLoopRunning = true; //Indicate thread is started
m_bListenerRunning = true; //Thread started, set control to true
CAsyncSocket listener; //Server listener awaits new connections
CAsyncSocket talker; //Socket for talking to client
char* buffer = "Welcome to RedEye Server v1.0\r\n"; //Welcome message
(will be variable later)

m_strStatus = "Running...";

/*Create the listener socket and set flags indicating
if Create() fails.*/
if(!listener.Create(m_nPort,SOCK_STREAM,NULL)){
m_bThreadLoopRunning = false;
m_bListenerRunning = false;
EnableControls(); //Must move out of thread later
return false;
}

EnableControls(); //Set buttons on dialog must move

//Set up listener to 'listen'. If the setup fails, set flags
// indicating that the setup failed, end thread etc.

if(!listener.Listen(5)){
m_bThreadLoopRunning = false;
m_bListenerRunning = false;
EnableControls(); //Must move out of thread
return false;
}

//While the flag remains true, attempt to accept any new
// connections into listener socket on socket 'talker'.
// If a connection is made, detach talker socket, start
// a new thread for the communication between the connected
// sockets, then go back to trying to accept new connections.
//A code review is likely needed here just in
 
 
 

CAsyncSocket in UI thread (Mr. Newcomer)

Post by Mike Durha » Tue, 20 Jan 2004 07:21:09

found that it worked if I used the MCF library in the dll's but it doesn't
work if I statically link.
Mike

"Pablo" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...
I
to
the
accepting
to
(see
'listener'
any
consideration
CAsyncSocket
with
I
dealing