Introduction
The Stream class is very useful to network programming because if you want to send some data over network you first need to pack some diffrent structs to one packet, then send it over network and afterwoods you have to reconstruct the old data structure:
datastructure -> stream -> send ->...network...-> receive -> stream -> datastructure
Output of Example program:
Test 1: seemless stream by using the end() method:
putting 11 bytes to stream...->"1________x"
putting 21 bytes to stream...->"2________xa________x"
putting 31 bytes to stream...->"3________x_________xb________x"
stream complete. Ready to send it over any device (eg. network)
deleting test data to make sure all data safely are in stream...
stream looks like:
-------------------------------------------------------------------
31 5F 5F 5F 5F 5F 5F 5F 5F 78 00 32 5F 5F 5F 5F 5F 5F 5F 5F 78 61
5F 5F 5F 5F 5F 5F 5F 5F 78 00 33 5F 5F 5F 5F 5F 5F 5F 5F 78 5F 5F
5F 5F 5F 5F 5F 5F 5F 78 62 5F 5F 5F 5F 5F 5F 5F 5F 78 00
-------------------------------------------------------------------
<63 Data Bytes, 63 Total>
Test 2: stream with header and block information by using the
endWidthBlockList() method:
putting 11 bytes to stream...->"1________x"
putting 21 bytes to stream...->"2________xa________x"
putting 31 bytes to stream...->"3________x_________xb________x"
stream complete. Ready to send it over any device (eg. network)
deleting test data to make sure all data safely are in stream...
stream looks like:
-------------------------------------------------------------------
03 00 00 00 0B 00 00 00 31 5F 5F 5F 5F 5F 5F 5F 5F 78 00 15 00 00
00 32 5F 5F 5F 5F 5F 5F 5F 5F 78 61 5F 5F 5F 5F 5F 5F 5F 5F 78 00
1F 00 00 00 33 5F 5F 5F 5F 5F 5F 5F 5F 78 5F 5F 5F 5F 5F 5F 5F 5F
5F 78 62 5F 5F 5F 5F 5F 5F 5F 5F 78 00
-------------------------------------------------------------------
<63 Data Bytes, 79 Total>
reconstruct old structure :
Number of blocks: 3
Stream Block Nr. = 1
Stream Block size = 11 bytes
Stream Block data = "1________x"
Stream Block Nr. = 2
Stream Block size = 21 bytes
Stream Block data = "2________xa________x"
Stream Block Nr. = 3
Stream Block size = 31 bytes
Stream Block data = "3________x_________xb________x"
done.
Using the code
The way you use this class is easy. You just have to make an instance of stream and then add data to it. When finished you have to call end
or endWithBlockList
to get the stream. The diffrence between end
and endWithBlockList
:
end()
will give you the complete Data in one Block without any header or block information. You will not be able to reconstruct the datastructure from this, but anyway you may need it if you don't need header and blockinformations (eg. voice over ip)
endWithBlockList()
will give you a complete stream with header and a list of blocks. With this you can reconstruct your data structure.
Here an example on how to use it if you want to send data over network:
Code on the first PC
Stream myStream;
myStream.begin();
myStream.add("Hello",6);
myStream.add("World",6);
myStream.add("!",2);
char * myData = (char *) myStream.endWithBlockList();
now you have to send the myData
over network and receive it on a second pc(i will not show you how to do that at this time ;-P)
now the code for the second Pc:
tStreamheader * pStreamheader = (tStreamheader *)myData;
unsigned long * pBlocksize;
char * pBlockdata = NULL;
unsigned long myoffset = sizeof(tStreamheader);
for (i = 0; i< (int) pStreamheader->numblocks; i++)
{
pBlocksize = (unsigned long *) (myData + myoffset);
myoffset+=sizeof(unsigned long);
pBlockdata = new char[*pBlocksize];
memmove(pBlockdata, myData + myoffset,*pBlocksize);
myoffset+=*pBlocksize;
printf("%s ",pBlockdata);
delete [] pBlockdata;
pBlockdata = NULL;
}
program on second PC will print out:
Hello World !
tStreamheader
is a struct for the header information of our stream. It contains only one variable at this time(unsigned long numblocks
) but it is easy to enhance it if you need more detailed information in the header like source, destination, time, totalsize,...
History
Version 1.0 released at 24.04.2003
1 May 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