Article View: borland.public.cppbuilder.internet.socket
Article #2938Re: TWinSocketStream Read in BYTE buffer
From: Remy Lebeau (TeamB)
Date: Fri, 23 Dec 2005 20:39
Date: Fri, 23 Dec 2005 20:39
171 lines
5442 bytes
5442 bytes
"Maurice Anderson" <mauriceanderson@hotmail.com> wrote in message
news:43ac3d34$1@newsgroups.borland.com...
> So lets say I send a file over, how would I 'convert' the file back
> to it's original format and save it to say the C drive.
Simply open a new file on the drive and then write the buffer into that
file. I strongly recommend that you send the file size along with the file
data so that the receiver knows ahead of time how much data to expect. For
example:
bool __fastcall ReadBuf(TWinSocketStream *Stream, void *Buffer, int
BufSize)
{
BYTE *pBuf = (BYTE*) Buffer;
int NumRead;
while( BufSize > 0 )
{
NumRead = Stream->Read(pBuf, BufSize);
if( NumRead < 1 )
return false;
pBuf += NumRead;
BufSize -= NumRead;
}
return true;
}
bool __fastcall WriteBuf(TWinSocketStream *Stream, void *Buffer, int
BufSize)
{
BYTE *pBuf = (BYTE*) Buffer;
int NumSent;
while( BufSize > 0 )
{
NumSent = Stream->Write(pBuf, BufSize);
if( NumSent < 1 )
return false;
pBuf += NumSent;
BufSize -= NumSent;
}
return true;
}
// sending the file ...
{
BYTE rbuf[256];
int bufsize;
// open a file ...
int datasize = ...; // get the file size
if( WriteBuf(str, &datasize, sizeof(int)) )
{
while( datasize > 0 )
{
bufsize = datasize;
if( bufsize > sizeof(rbuf) )
bufsize = sizeof(rbuf);
// read from the file into rbuf, up to no more than bufsize
bytes ...
// update bufsize with the number of bytes actually read ...
if( !WriteBuf(str, rbuf, bufsize) )
break;
datasize -= bufsize;
}
}
// close the file ...
}
// reading the file ...
{
BYTE rbuf[256];
int bufsize, datasize = 0;
if( ReadBuf(str, &datasize, sizeof(int)) )
{
bool error = false;
// open a file ...
// preallocate the file to datasize bytes ...
while( datasize > 0 )
{
bufsize = datasize;
if( bufsize > sizeof(rbuf) )
bufsize = sizeof(rbuf);
if( !ReadBuf(str, rbuf, bufsize) )
{
error = true;
break;
}
// write rbuf to the file, up to no more than bufsize bytes
...
datasize -= bufsize;
}
// close the file ...
if( error )
// delete incomplete file ...
}
}
> Or lets say I sent a simple text message over as "Hello World".
> How do I recover the messages from rbuf?
If the contents of the buffer are null-terminated, then you can cast the
buffer to a char*:
AnsiString msg = (char*) rbuf;
If it is not null-terminated, you can pass the buffer to one of AnsiString's
constructors that takes a length as a second parameter.
AnsiString msg((char*) rbuf, number of characters in buffer);
Again, I highly recommend that you send the string length along with the
string data. For example:
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 ReadString(TWinSocketStream *Stream, AnsiString &S)
{
int length = 0;
if( ReadBuf(Stream, &length, sizeof(int)) )
{
S.SetLength(length);
return ReadBuf(Stream, S.c_str(), length);
}
return false;
}
// sending a message ...
if( !WriteString(Stream, "Hello World!") )
// error ...
// reading a message ...
AnsiString msg;
if( ReadString(Stream, msg) )
// use msg as needed ...
else
// error ...
Gambit
Technical Details
Message-ID:
<43ac6028$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!atl-c02.usenetserver.com!news.usenetserver.com!atl-c07.usenetserver.com!news.usenetserver.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<43ac3d34$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> Subject: Re: TWinSocketStream Read in BYTE buffer Date: Fri, 23 Dec 2005 12:39:41 -0800 Lines: 173 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: <43ac6028$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1135370280 68.69.17.41 (23 Dec 2005 12:38:00 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!atl-c02.usenetserver.com!news.usenetserver.com!atl-c07.usenetserver.com!news.usenetserver.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail