Introduction

This article shows you how to serialize a class or standard variable over a TCP/IP socket connection. It shows an example of how to setup the TCP/IP connection, setup other needed classes, and send/receive data over the socket.

I pulled from the MFC class CSocketFile. As you may know, CSocketFile lets you send and receive data to/from a network connection using a CArchive. The drawback is that the implementation of CSocket that is used has 16 bit code in it - not good for any decent application.

So I basically re-wrote them using CBlockingSocket (instead of CSocket) which uses 32 bit code and is better optimized. This allows you to attach a CArchive to an open CSocketFile and then serialize data to and from it (thus sending an receiving the data across the network).

Details

Here are the basic steps:

Create and connect a CBlockingSocket object to the computer you want to send/receive data with

Create a CBlockingSocketFile and attach it to the CBlockingSocket object you created in step 1 above.

Create a CArchive object and attach it to the CBlockingSocketFile object you created in step 2 above. NOTE: You must create 1 CArchive for sending and 1 for receiving.

Send your data or classes using the CArchive object created in step 3. You can send any serializable class or raw variables. VERY IMPORTANT NOTE: make sure you flush the CArchive object after you’re done sending data. This ensures all data is written across the network and tells the socket at the other end to go ahead and receive it.

Close out the CArchive objects and the CBlockingSocket object. NOTE: Don’t close the CBlockingSocketFile object or it will assert.

Conclusion

That’s basically it. Of course, I’m making one assumption that there’s a listening socket on the other end ready to send/receive the data. For convenience I’ve added an appendix below with the code to setup a listening (server) socket and receive the data then send an acknowledgment.

In the future I may derive from CArchive and add functionality to make this whole process even easier, but for now I think this is pretty straight forward. Enjoy and happy coding!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Share

About the Author

Became hooked on programming in the 3rd grade using basic. Programming in C/C++ since 1997.

This year I worked on a three person team to create a distributed computing system that predicts stock prices using an Artificial Neural Network. We used Encog as the ANN framework and Scoop as the distributed computing framework.

Prior to that, I created a proof of concept augmented reality game engine that runs on mobile platforms. The aim of that project was to create a MMO augmented reality system that would allow users to create their own game scenarios and virtual worlds in an augmented reality space. The project was coded using Objective-C, OpenGL, REST web-services and MySQL.

Can you tell me whether this design is efficient in terms of the server's capacity to handle connections?

For example, i have noticed that web-server applications listen on port 80, but only for sake of initial contact. After initial contact is made, they move the conversation to another port so they can keep port 80 open for new conversations. (At least thats how i think those applications work, but i am not really an expert.)

Would the server side of this application be more efficient if it were elaborated in such a manner? Or is there no real benefit to elaborating the server side of this application in this manner?

I'm pretty new at this but it seems to me that sending pointers would be completely useless since even though you could send them the memory adresses they reference would contain other data and the pointer would therefore be useless.

Hello,
First of all, please excuse me for the long introduction.
I am currently working on a network-based control application for a video-on-demand server. To control the server, I serialize a command object through the CSocket using CSocketFile/CArchive.
If I serialize lets say 5 objects in intervals of about 100-1000ms and put a breakpoint in the OnReceive of the opposed socket, I only receive the first and (after hitting continue) the last object. This doesn't really matter in the running application, as I could hard-code an optimal interval between the sent objects, _but_ when debugging the application this is really getting on my nerves.

Now I wanted to give the CBlockingSocket a try because I thought it could be a bit better

To cut a long story short, my question is:
How do I implement the OnReceive/OnClose/OnAccept virtual members of the CSocket class in CBlockingSocket?
And does anybody have a solution to my first problem? I always thought that MFC/Winsock grants that with CSocket, every data is received as it was sent. So what am I doing wrong?

Yes, there's no Serialize() member function in CString class.
However it still has serialization feature owing to CString::operator <<, >>.
I'd like to use that feature
I quote a snippet from MSDN Library;