Article View: borland.public.cppbuilder.internet.socket
Article #2961Re: TWinSocketStream Read in BYTE buffer
From: Remy Lebeau (TeamB)
Date: Thu, 29 Dec 2005 21:32
Date: Thu, 29 Dec 2005 21:32
315 lines
10263 bytes
10263 bytes
"newsgroups.borland.com" <mauriceanderson@hotmail.com> wrote in message
news:43b43a5a$1@newsgroups.borland.com...
> TMemoryStream *DataStream = new TMemoryStream;
Why are you using TMemoryStream instead of TFileStream?
> DataStream->Write(rbuf, bufsize);
> <----- What I added
You added it in the wrong place. You did not put it where I told you to put
it. I put comments in the code to tell you where to perform various
operations, but you seem to be ignoring them completely.
Here is a more complete code snippet for you:
bool __fastcall ReadBuf(TWinSocketStream *Stream, void *Buffer, int
BufSize)
{
BYTE *pBuf = (BYTE*) Buffer;
int NumRead;
try
{
while( BufSize > 0 )
{
NumRead = Stream->Read(pBuf, BufSize);
if( NumRead < 1 )
return false;
pBuf += NumRead;
BufSize -= NumRead;
}
}
catch(const Exception &)
{
return false;
}
return true;
}
bool __fastcall WriteBuf(TWinSocketStream *Stream, void *Buffer, int
BufSize)
{
BYTE *pBuf = (BYTE*) Buffer;
int NumSent;
try
{
while( BufSize > 0 )
{
NumSent = Stream->Write(pBuf, BufSize);
if( NumSent < 1 )
return false;
pBuf += NumSent;
BufSize -= NumSent;
}
}
catch(const Exception &)
{
return false;
}
return true;
}
bool __fastcall ReadString(TWinSocketStream *Stream, AnsiString &S)
{
int length = 0;
if( ReadBuf(Stream, &length, sizeof(int)) )
{
if( length > 0 )
{
try
{
S.SetLength(length);
}
catch(const Exception &)
{
return false;
}
return ReadBuf(Stream, S.c_str(), length);
}
else
{
S = "";
return true;
}
}
return false;
}
bool __fastcall WriteString(TWinSocketStream *Stream, const AnsiString
&S)
{
int length = S.Length();
if( WriteBuf(Stream, &length, sizeof(int)) )
return WriteBuf(Stream, S.c_str(), length);
return false;
}
bool __fastcall SendTextMessage(TWinSocketStream *Stream, const
AnsiString &Msg)
{
char type = 0x01;
if( WriteBuf(Stream, &type, sizeof(char)) )
return WriteString(Stream, Msg) )
return false;
}
bool __fastcall SendFile(TWinSocketStream *Stream, const AnsiString
&FileName)
{
bool error = false;
try
{
TFileStream *FS = new TFileStream(FileName, fmOpenRead |
fmShareDenyWrite);
try
{
char type = 0x02;
if( WriteBuf(str, &type, sizeof(char)) )
{
if( WriteString(str, ExtractFileName(FileName)) )
{
int size = FS->Size;
if( WriteBuf(str, &size, sizeof(int)) )
{
BYTE rbuf[256];
int bufsize;
while( size > 0 )
{
bufsize = size;
if( bufsize > sizeof(rbuf) )
bufsize = sizeof(rbuf);
bufsize = FS->Read(rbuf, bufsize);
if( bufsize < 1 )
{
error = true;
break;
}
if( !WriteBuf(Stream, rbuf, bufsize) )
{
error = true;
break;
}
size -= bufsize;
}
}
}
}
}
__finally
{
delete FS;
}
}
catch(const Exception &)
{
error = true;
}
return !error;
}
bool __fastcall ReadFile(TWinSocketStream *Stream, AnsiString &FileName)
{
AnsiString fn;
if( ReadString(Stream, fn) )
{
fn = ("C:\\Some Folder\\" + ExtractFileName(fn));
int size = 0;
if( ReadBuf(str, &size, sizeof(int)) )
{
bool error = false;
try
{
TFileStream *FS = new TFileStream(fn, fmCreate);
try
{
FS->Size = size;
BYTE rbuf[256];
int bufsize;
while( size > 0 )
{
bufsize = size;
if( bufsize > sizeof(rbuf) )
bufsize = sizeof(rbuf);
if( !ReadBuf(rbuf, bufsize) )
{
error = true;
break;
}
if( FS->Write(rbuf, bufsize) < 1 )
{
error = true;
break;
}
size -= bufsize;
}
}
__finally
{
delete FS;
}
}
catch(const Exception &)
{
error = true;
DeleteFile(fn);
}
if( !error )
{
FileName = fn;
return true;
}
}
}
return false;
}
bool __fastcall ReadPacket(TWinSocketStream *Stream)
{
char type = 0x00;
if( ReadBuf(Stream, &type, sizeof(char)) )
{
switch( type )
{
case 0x01: // text
{
AnsiString Msg;
if( ReadString(Stream, Msg) )
{
// use msg as needed ...
return true;
}
break;
}
case 0x02: // file
{
AnsiString FileName;
if( ReadFile(Stream, FileName) )
{
// use FileName as needed...
return true;
}
break;
}
default: // unknown type
{
// unknown data received, do something...
break;
}
}
}
return false;
}
> The contents of 'maurice.ini', is garbled.
You did not show the code you used to send the data. But since you are
writing the buffer to the stream in the wrong place anyway, you are likely
writing a buffer that has not been initialized yet.
> Also, I have a quick question about function ReadBuf(TStream *Strm,
> void *Buffer, int Count). In the function you have
> pBuf += NumRead. Why are you adding the number of bytes
> read to our data?
I am incrementing the pointer to the data, not appending the number to the
data itself. The pointer is incremented to point to a new memory location
inside the Buffer.
For example, say a 10-byte Buffer is passed to ReadBuf(). Then say that
TWinSocketStream::Read() receives 5 bytes the first time it is called. The
first 5 bytes of the Buffer now contain data. pBuf is then incremented by 5
bytes so that the next call to TWinSocketStream::Read() will receive the
next block of incoming data beginning at the 6th byte of the Buffer. And so
on.
The same applies to WriteBuf() as well. Again, with a 10-byte Buffer passed
in, if TWinSocketStream::Write() sends 5 bytes the first time, then the next
call to Write() has to start at the 6th byte in the Buffer. And so on.
Gambit
Technical Details
Message-ID:
<43b45550$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!news-out.newsfeeds.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!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> <43b252fa$1@newsgroups.borland.com> <43b43a5a$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: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> 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> <43b252fa$1@newsgroups.borland.com> <43b43a5a$1@newsgroups.borland.com> Subject: Re: TWinSocketStream Read in BYTE buffer Date: Thu, 29 Dec 2005 13:32:09 -0800 Lines: 317 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 68.69.17.41 Message-ID: <43b45550$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1135891792 68.69.17.41 (29 Dec 2005 13:29:52 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!news-out.newsfeeds.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail