Article View: borland.public.cppbuilder.internet.socket
Article #2946Re: TWinSocketStream Read in BYTE buffer
From: Jonathan Benedicto
Date: Tue, 27 Dec 2005 20:38
Date: Tue, 27 Dec 2005 20:38
110 lines
4449 bytes
4449 bytes
Maurice Anderson wrote:
> I am "trying" to implement your idea to send the type(binary or text)
> and byte size to the server for pre-processing so that it knows how
> to handle the data sent. So for binary data, I want to server to
> save the data back as a file in it's original format and for text, I
> want to display it as a popup message or something. So the 'frame'
> would look something like this format: [type code][data length][file
> name][data]. So my code would look something like this:
I'd make a "virtual" frame, where the data is never written as a frame, but
it written in a more space efficient method. Like this:
// untested.
To write a file "frame" :
int TypeCode = 0x01;
int FileNameLength;
int DataStreamLength;
AnsiString FileName;
TMemoryStream *DataStream;
FileName = "test.txt";
DataStream = new TMemoryStream;
DataStream->LoadFromFile( "C:\test.txt" );
FileNameLength = FileName.Length();
DataStreamLength = DataStream->Size;
WriteBuf( str, &TypeCode, sizeof( int ) );
WriteBuf( str, &FileNameLength, sizeof( int ) );
WriteBuf( str, FileName.c_str(), FileName.Length() );
WriteBuf( str, &DataStreamLength, sizeof( int ) );
WriteBuf( str, DataStream->Memory, DataStream->Size );
To write a text "frame" :
int TypeCode = 0x02;
int TextLength;
AnsiString Text;
Text = "Hello";
TextLength = Text.Length();
WriteBuf( str, &TypeCode, sizeof( int ) );
WriteBuf( str, &TextLength, sizeof( int ) );
WriteBuf( str, Text.c_str(), Text.Length() );
Now, to read this back would be a touch more difficult.
void __fastcall ReadData( TWinSocketStream *PStream )
{
int TypeCode;
if( !ReadBuf( PStream, &TypeCode, sizeof( int ) ) )
return;
if( TypeCode == 0x01 )
{
// A file.
int FileNameLength;
int DataStreamLength;
AnsiString FileName;
TMemoryStream *DataStream;
if( ReadBuf( PStream, &FileNameLength, sizeof( int ) ) &&
FileNameLength > 0 && FileNameLength < MAX_PATH )
{
FileName.SetLength( FileNameLength );
if( ReadBuf( PStream, FileName.c_str(), FileName.Length() ) &&
FileName != "" )
{
if( ReadBuf( PStream, &DataStreamLength, sizeof( int ) &&
DataStreamLength > 0 )
{
DataStream->Size = DataStreamLength;
if( ReadBuf( PStream, DataStream->Memory,
DataStream->Size ) )
{
// file received, so save it.
DataStream->SaveToFile( FileName );
}
}
}
}
}
else if( TypeCode == 0x02 )
{
// Text.
int TextLength;
AnsiString Text;
if( ReadBuf( PStream, &TextLength, sizeof( int ) ) && TextLength >
0 )
{
Text.SetLength( TextLength );
if( ReadBuf( PStream, Text.c_str(), Text.Length() ) && Text !=
"" )
{
// text received.
ShowMessage( Text );
}
}
}
}
HTH
Jonathan
http://jomitech.com/mtbcc32.shtml - Multi-threaded compilation for BCB
http://jomitech.com/forum - JomiTech Forums
Technical Details
Message-ID:
<43b1a5d6$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<43ac3d34$1@newsgroups.borland.com> <43ac6028$1@newsgroups.borland.com> <43acc4c5$1@newsgroups.borland.com> <43ae5b51$1@newsgroups.borland.com> <43aeebe1$1@newsgroups.borland.com> <43aef70e$1@newsgroups.borland.com> <43af06d7$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,74cdc31c66b3abd9 X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Jonathan Benedicto" <incorrect@no.server> Newsgroups: borland.public.cppbuilder.internet.socket References: <43ac3d34$1@newsgroups.borland.com> <43ac6028$1@newsgroups.borland.com> <43acc4c5$1@newsgroups.borland.com> <43ae5b51$1@newsgroups.borland.com> <43aeebe1$1@newsgroups.borland.com> <43aef70e$1@newsgroups.borland.com> <43af06d7$1@newsgroups.borland.com> Subject: Re: TWinSocketStream Read in BYTE buffer Date: Tue, 27 Dec 2005 15:38:28 -0500 Lines: 112 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 69.158.119.7 Message-ID: <43b1a5d6$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1135715798 69.158.119.7 (27 Dec 2005 12:36:38 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail