From: CBudz on
Hello, I have a MFC app source and am adding some TCP/IP functionality. The
current design is not multithreaded and sending/receiving sockets interferes
with timeouts (waiting for client to connect, receiving messages, etc.). I'd
like to spawn a thread to handle the TCP/IP then signal an event and the app
will take care of the rest. I'm new to MFC; have never used it before
looking at this app's source. Can I create a worker thread to handle my
socket communications?

Thanks,
Chris
From: Hector Santos on
CBudz wrote:

> Hello, I have a MFC app source and am adding some TCP/IP functionality. The
> current design is not multithreaded and sending/receiving sockets interferes
> with timeouts (waiting for client to connect, receiving messages, etc.). I'd
> like to spawn a thread to handle the TCP/IP then signal an event and the app
> will take care of the rest. I'm new to MFC; have never used it before
> looking at this app's source. Can I create a worker thread to handle my
> socket communications?


Yes, but have you looked and tried using the MFC class CAsyncSocket?

This gives you asynchronous behavior (it has its own thread logic) for
your MFC GUI (main) thread which handles your messages. It allows
you to create handlers like

OnAccept (for server applications)
OnConnect (for client applications)
OnReceive
OnSend
OnClose

that you can add to your GUI MFC main window class frame or dialog.

Otherwise, you have to do the same sort of thing using raw sockets,
spawn a thread and send messages to your MFC window, etc.

Look at using CAsyncSocket.

--
HLS
From: Scott McPhillips [MVP] on
"CBudz" <CBudz(a)discussions.microsoft.com> wrote in message
news:DD130077-0755-4705-891A-685040F4B879(a)microsoft.com...
> Hello, I have a MFC app source and am adding some TCP/IP functionality.
> The
> current design is not multithreaded and sending/receiving sockets
> interferes
> with timeouts (waiting for client to connect, receiving messages, etc.).
> I'd
> like to spawn a thread to handle the TCP/IP then signal an event and the
> app
> will take care of the rest. I'm new to MFC; have never used it before
> looking at this app's source. Can I create a worker thread to handle my
> socket communications?
>
> Thanks,
> Chris

Certainly you can create a worker thread in MFC. Call AfxBeginThread with a
thread function pointer.

Your mention of timeout delays implies you are thinking in terms of blocking
sockets. But winsock (and MFC) have a better way. You can use CAsyncSocket
to create a socket that never blocks. Lengthy operations are handled by an
operating system thread and it sends you notifications when their results
are ready. This makes it generally unnecessary to put the sockets in a
secondary thread: They can happily coexist within the GUI thread.

There are articles on both of these subjects at
http://www.flounder.com/mvp_tips.htm

--
Scott McPhillips [VC++ MVP]

From: Joseph M. Newcomer on
Define "wasting time". If you are using synchronous sockets, your design is wrong. Change
it to use asynchronous sockets and many of the problems will go away. Synchronous
sockets are a bad design decision, and MFC's CSocket class has fatal bugs (I've never used
it but those who have say that it has serious problems that cause failures).

In addition, if you want to move the sockets to separate threads, you need to read my
essays (on my MVP Tips site) on asynchronous sockets (the Microsoft example was written by
someone who didn't understand threading, sockets, or synchronization, but other than those
minor flaws, their code can be used if you don't mind totally rewriting it, which I have
done), and my essay on UI threads. Key here is you have to unwrap the "bare socket
handle" in one thread ahd rewrap it in the thread that is going to use it, which I explain
in detail.
joe

On Wed, 31 Mar 2010 15:21:01 -0700, CBudz <CBudz(a)discussions.microsoft.com> wrote:

>Hello, I have a MFC app source and am adding some TCP/IP functionality. The
>current design is not multithreaded and sending/receiving sockets interferes
>with timeouts (waiting for client to connect, receiving messages, etc.). I'd
>like to spawn a thread to handle the TCP/IP then signal an event and the app
>will take care of the rest. I'm new to MFC; have never used it before
>looking at this app's source. Can I create a worker thread to handle my
>socket communications?
>
>Thanks,
>Chris
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: CBudz on
Thanks Hector, Scott, and Joe for your replies. I'm used to writing sockets
for UNIX and followed the same method; the sockets work great (meaning I get
my data without any problems). I will rewrite it if it's absolutely
necessary but I think spawning a new thread will solve my issue. I should
have been more specific about the app. The app is to manage an embedded
system, the GUI has attached devices states and sensor readings. The code
goes through a series of calls that need to run multiple times a second and I
do not know when the client wants to connect so listening will crash the
code. Since I can spawn a thread to handle it I can update strings and
process on the next cycle.

Thanks again for the help,
Chris