Click here to Skip to main content
15,886,873 members
Articles / Programming Languages / C++

IPCWorkshop

Rate me:
Please Sign up or sign in to vote.
4.91/5 (43 votes)
29 Jan 20037 min read 156.6K   4.5K   93  
This article describes a data transfer over various IPC mechansisms
#include "stdafx.h"
#include "DerivedSocket.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


UINT CDerivedSocket::m_nTransferMessage = NULL;

////////////////////////////////////////////////////////////////////////////////
// Constructor / Destructor													  //
////////////////////////////////////////////////////////////////////////////////
CDerivedSocket::CDerivedSocket()
{
	m_nDataSize = 0;
}


CDerivedSocket::~CDerivedSocket()
{
}


////////////////////////////////////////////////////////////////////////////////
// Operations                                                                 //
////////////////////////////////////////////////////////////////////////////////
void CDerivedSocket::Initialize(bool pIsServer, LPCTSTR pHostAddress, UINT pHostPort)
{
	AfxSocketInit();
	// Creating a stream socket.
	// socket server
	if( pIsServer) {
		if( Create(pHostPort) == 0)
			TRACE("Unable to create a socket. Error is %d", GetLastError());		
	}
	else{	// socket client
		if( Create() == 0)
			TRACE("Unable to create a socket. Error is %d", GetLastError());
		if( Connect(pHostAddress, pHostPort) == 0)
			TRACE("Unable to connect to a socket. Error is %d", GetLastError());
	}
}

// Overrided Notification Function
void CDerivedSocket::OnReceive(int nErrorCode)
{
	CSocket::OnReceive(nErrorCode);
	ASSERT( m_nDataSize != 0 );
	char* buff = new char[m_nDataSize];
	Receive(buff, m_nDataSize);
	// Notify that the message is received.
	::PostMessage( HWND_BROADCAST, m_nTransferMessage, (WPARAM)buff, m_nLParam);
}

// Overrided Notification Function
void CDerivedSocket::OnAccept(int nErrorCode)
{
	CSocket::OnAccept(nErrorCode);
	CDerivedSocket* This = new CDerivedSocket();
	This->m_nDataSize = m_nDataSize;
	This->m_nLParam = m_nLParam;
	Accept(*This);	
}

// Collect the RegisterWindow message Id, inorder to notify
void CDerivedSocket::Listen(UINT pOnReciveId, LPARAM pParam)
{
	m_nLParam = pParam;
	m_nTransferMessage = pOnReciveId;
	CSocket::Listen();
}

// Sends data to the server
bool CDerivedSocket::Send(char* pData, int pLength)
{
	int bytesSent = 0;
	bytesSent = CSocket::Send( pData, pLength);
	if( bytesSent <= 0 ) {
		TRACE("Unable to send data. Error is %d", GetLastError());
		return false;
	}
	return true;		
}

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 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.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
Iam a software developer from Chennai, India. I've been working mainly under windows environment.I am attracted to various programming languages including : C/C++, Python. I've been programming VC++/MFC, ATL/COM, Lex & Yacc and database design/development ( SQL/Oracle ) for the past two years.



Comments and Discussions