Click here to Skip to main content
15,885,244 members
Articles / Desktop Programming / MFC

Serializing classes across a network socket (TCP/IP)

Rate me:
Please Sign up or sign in to vote.
4.64/5 (11 votes)
7 Mar 2004Public Domain2 min read 98.2K   2.4K   37  
This article gives example and code for sending a class across a network connection using TCP/IP and the CBlockingSocket class.
// BlockingSocketFile.h: interface for the CBlockingSocketFile class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_BLOCKINGSOCKETFILE_H__61F83C6F_B0DD_4622_8004_36B0F9A70E12__INCLUDED_)
#define AFX_BLOCKINGSOCKETFILE_H__61F83C6F_B0DD_4622_8004_36B0F9A70E12__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <Blocksock.h>

#define BLOCK_SOCK_TIMEOUT 15

class AFX_EXT_CLASS CBlockingSocketFile : public CFile  
{
	DECLARE_DYNAMIC(CBlockingSocketFile)
public:
//Constructors
	CBlockingSocketFile(CBlockingSocket *pNewSocket);
	virtual ~CBlockingSocketFile();

// Implementation
public:
	void RegisterLog( void (*newLog)(const char *) );
	CBlockingSocket * pSocket;

	virtual UINT Read(void * pBuffer, UINT nBytes);
	virtual void Write(const void* pBuffer, UINT nBytes);
	virtual void Close();

private:
	void (*Log)(const char *); // pointer to logging function
// Unsupported APIs
	virtual BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL);
	virtual CFile* Duplicate() const;
	virtual DWORD GetPosition() const;
	virtual LONG Seek(LONG lOff, UINT nFrom);
	virtual void SetLength(DWORD dwNewLen);
	virtual DWORD GetLength() const;
	virtual void LockRange(DWORD dwPos, DWORD dwCount);
	virtual void UnlockRange(DWORD dwPos, DWORD dwCount);
	virtual void Flush();
	virtual void Abort();
};

#endif // !defined(AFX_BLOCKINGSOCKETFILE_H__61F83C6F_B0DD_4622_8004_36B0F9A70E12__INCLUDED_)

/*
/////////////////////////////////////////////////////////////////////////////
// CSocketFile

class CSocketFile : public CFile
{
	DECLARE_DYNAMIC(CSocketFile)
public:
//Constructors
	CSocketFile(CSocket* pSocket, BOOL bArchiveCompatible = TRUE);

// Implementation
public:
	CSocket* m_pSocket;
	BOOL m_bArchiveCompatible;

	virtual ~CSocketFile();

#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif
	virtual UINT Read(void* lpBuf, UINT nCount);
	virtual void Write(const void* lpBuf, UINT nCount);
	virtual void Close();

// Unsupported APIs
	virtual BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL);
	virtual CFile* Duplicate() const;
	virtual DWORD GetPosition() const;
	virtual LONG Seek(LONG lOff, UINT nFrom);
	virtual void SetLength(DWORD dwNewLen);
	virtual DWORD GetLength() const;
	virtual void LockRange(DWORD dwPos, DWORD dwCount);
	virtual void UnlockRange(DWORD dwPos, DWORD dwCount);
	virtual void Flush();
	virtual void Abort();
};

*/

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer (Senior)
United States United States
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.

Fluent in the following languages and frameworks:
PHP
Python
HTML and CSS
Javascript
JQuery
MySQL
C / C++
Objective-C
FANN (ANN Framework)
Encog (ANN Framework)
Scoop (Distributed Computing Framework as a Python module)
iOS Framework
Cake PHP
Django

Comments and Discussions