Click here to Skip to main content
15,895,256 members
Articles / Desktop Programming / MFC

Network Stream Class

Rate me:
Please Sign up or sign in to vote.
3.27/5 (10 votes)
6 May 20032 min read 56.4K   971   25  
A stream class to prepare data to be sent over the network
// File		: Main.cpp
// Author	: Hans Hamm alias Sendel
// History	: 01.05.2003 v 1.1
//					- more efficient (you can create the stream as often you need, not only one time)
//                  - easier to use (you dont need say that you want to "BEGIN" a stream - so the method "begin()" was removed)
//					- there are no known bugs to be fixed, so no bugfix needed :-P
//          : 24.04.2003 v 1.0
// What		: Main Program
//////////////////////////////////////////////////////////////////////

#include "stream.h"
#include <conio.h>	// for getch()
#include <stdlib.h> // for malloc, free, printf
#include <string.h> // for memmove

//////////////////////////////////////////////////////////////////////////////
// 
// Some Functions to make maincode smaller:
// 
//////////////////////////////////////////////////////////////////////////////
void fillDataToStream(Stream &myStream);
void printstream(char * myData, int datasize, int totalsize);

//////////////////////////////////////////////////////////////////////////////
//****************************************************************************
//* 
//*   MAIN PROGRAM
void main()
{
	printf("start.\n");

/////////////////////////////////////////////////////////////////////////
//
//  Stream Test
	Stream myStream;
	int datasize;
	int buffersize;
	char * myData;
	char * myBuffer;

	// fill in some test data:
	fillDataToStream(myStream);
	datasize = (int) myStream.getDataSize();
	buffersize = (int) myStream.getBufferSize();

	myData = new char[datasize];
	myBuffer = new char[buffersize];
	
	myStream.getData(myData);
	myStream.getBuffer(myBuffer);

	// we can clear stream now, because all needed data is stored in myData / myBuffer...
	myStream.clear();

	// print out current data stream:
	printf("Stream without header or block information:\n");
	printstream(myData, datasize,datasize);
	// print out current buffer stream:
	printf("Stream with header and block information:\n");
	printstream(myBuffer, datasize,buffersize);

	// no need for myData anymore so release it...
	delete [] myData;
	myData = NULL;
	
	//////////////////////////////////////////////////////////////////////
	// Reconstruct old structure: 
	//////////////////////////////////////////////////////////////////////

	printf(" reconstruct old structure :\n");
	// get headerinformation...
	tStreamheader * pStreamheader = (tStreamheader *)myBuffer;
	// how many blocks ?
	printf(" Number of blocks: %i\n",pStreamheader->numblocks);
	
	// get data of each block
	unsigned long * pBlocksize;
	char * pBlockdata = NULL;
	// we need a pointer to step through our stream
	unsigned long myoffset = sizeof(tStreamheader); // beginning directly after header...
	
	// now we can make a loop where we can rebuild each block:
	for (int i = 0; i< (int) pStreamheader->numblocks; i++) // all blocks to be processed
	{
		pBlocksize = (unsigned long *) (myBuffer + myoffset); // get our blocksize of next block
		myoffset+=sizeof(unsigned long); // go to it
		pBlockdata = new char[*pBlocksize]; // make room for block data
		memmove(pBlockdata, myBuffer + myoffset,*pBlocksize); // copy the block data
		myoffset+=*pBlocksize; // step to next block (blocksize+blockdata)

		// print out what we got :
		printf(" Stream Block Nr.  = %i\n", i+1);
		printf(" Stream Block size = %u bytes\n",*pBlocksize);
		printf(" Stream Block data = \"%s\"\n",pBlockdata);
		
		delete [] pBlockdata; // we don't need the data anymore...
		pBlockdata = NULL;
	}

	// no need for myBuffer anymore, so we can release the allocated memory:
	delete [] myBuffer;
	myBuffer = NULL;
//
//  End ofStream Test
/////////////////////////////////////////////////////////////////////////

	printf("done.\n");
	getch();
}
//* END OF MAIN PROGRAM
//****************************************************************************
//////////////////////////////////////////////////////////////////////////////



//////////////////////////////////////////////////////////////////////////////
// 
// IMPLEMENTATION OF PRE
// 
//////////////////////////////////////////////////////////////////////////////

void fillDataToStream(Stream &myStream)
{
	// test data:
	// three strings with diffrent lenghts: 10, 20 and 30.
	// but we don't have to forget the terminating zero at the end of the strings, so we need
	// one more byte -> strlen(str) + 1
	// fill in some data to stream:
	char * str;
	int size;
	str = "1234567890\0";						size = strlen(str)+1; myStream.add(str,size); printf(" putting %i bytes to stream...->\"%s\"\n",size, str);
	str = "abcdefghijklmnopqrst\0";				size = strlen(str)+1; myStream.add(str,size); printf(" putting %i bytes to stream...->\"%s\"\n",size, str);
	str = "www.CodeProject.com\0";	size = strlen(str)+1; myStream.add(str,size); printf(" putting %i bytes to stream...->\"%s\"\n",size, str);
}

void printstream(char * myData, int datasize, int totalsize)
{
	printf(" stream looks like:\n");
	printf(" -------------------------------------------------------------------\n  ");
	int i;
	unsigned char c;
	for (i=0; i<totalsize; i++)
	{
		if ((i>1) && (i%16==0)) printf("\n  "); // to make look nicer ;-P
		c = (unsigned char) myData[i];
		printf(" %02X ",c); // printout our value in hex
	}
	printf("\n -------------------------------------------------------------------\n");
	printf(" <%0000i Data Bytes, %0000i Total Bytes>\n",datasize,totalsize);
	if (datasize != totalsize)
	{
		printf(" <%0000i Header Bytes>\n",totalsize - datasize);
	}
}

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
Software Developer (Senior) Deep Silver FISHLABS
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions