Re: Regarding CArchive,Files and CSockets

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



"James Simpson" <JamesSimpson@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:81B75D88-F3C1-431A-AB71-5A57F80AC302@xxxxxxxxxxxxxxxx
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]

.



Relevant Pages

  • Re: Regarding CArchive,Files and CSockets
    ... If you are thinking of using threads and sockets, see my essay on multithreaded sockets. ... Forget CSocketFile for the same reason. ... Essentially each connection on the client/server side uses this type of code: ... I can send a CString and some basic data types by simply using << ...
    (microsoft.public.vc.mfc)
  • Re: MFC Interview Tests
    ... None of them are an MFC question. ... How would you implement user-defined messages in an MFC app? ... Why don't you need LPCTSTR casts to pass a CString to most API calls? ... Asynchronous sockets in a single separate thread ...
    (microsoft.public.vc.mfc)
  • CAsyncSocket in service
    ... I have an MFC application that has been running well for a long time. ... The client now wants it converted to run as a service, interfacing ... the sockets do not seem to run. ... CWinThread does not seem to be running a message pump. ...
    (microsoft.public.vc.mfc)
  • Re: Sockets and Notification
    ... I use synchronous sockets exclusively (i.e. native WinSock2 stuff with send ... and recv) - no MFC CAsyncSocket stuff. ... If you want to wait for a predetermined time on a recv call (note - the ... > Anyway, Lets say I want to call a method in the communication class, tell ...
    (microsoft.public.vc.mfc)
  • Re: Looking for a NIO client/server example
    ... "Chris Berg"??? ... > My client/server application works with sockets, one for each user, ... > the sockets remain open for the entire session. ... > 2) I need to understand NIO in more detail. ...
    (comp.lang.java.programmer)