Re: Regarding CArchive,Files and CSockets
"James Simpson" <JamesSimpson@discussions.microsoft.com> wrote in message
news:81B75D88-F3C1-431A-AB71-5A57F80AC302@microsoft.com...
Hello again,
I am trying to work on creating the client/server portion of my solution
via
MFC. I think I got a handle on threading and sockets a bit so far.
Essentially each connection on the client/server side uses this type of
code:
CSocket CurSocket;
CurSocket.Create();
CSocketFile CurSocketFile(&CurSocket,TRUE);
CArchive arSend(&CurSocketFile,CArchive::store);
CArchive arRecv(&CurSocketFile,CArchive::load);
CurSocket.Connect("localhost",1337);
(e.g. it creates a socket if it doesn't have one, and attaches the
CSocketFile and CArchive classes to allow data to be streamed into out of
the
socket). I can send a CString and some basic data types by simply using
<<
and calling flush afterwards to send the data and use >> on the receiving
end
to receive the data. The problem that I have, however is sending files
using
CArchive interface. How do you use CArchive to send the data to the
server
or client?
With the CArchive method you can send any object that is derived from
CObject and supports serialization. One way to send a file would be to put
the data into a CByteArray and serialize that.
Years ago I first learned about sockets in MFC the way you are going: with
CSocket/CSocketFile/CArchive. Then I was very unhappy with the result.
That architecture has been buggy for years and seems too complex and
convoluted to ever be fixed. I ripped it all out and went with CAsyncSocket
and packing/unpacking my own message format. One big problem with CSocket
is that its read and write calls don't return until they have completed.
But in a Windows application you really want them to return pretty soon no
matter what happens. My original code would get stuck (somehow) with both
sides waiting for the other to send something. I.e., both sides were inside
Receive and never came out. If I were you I would forget about
CSocket/CSocketFile/CArchive and go with the much "thinner" CAsyncSocket.
It is much easier to understand and it works great. And the things you have
to learn are applicable to all sockets, rather than just to MFC.
--
Scott McPhillips [VC++ MVP]