Article View: borland.public.cppbuilder.internet.socket
Article #2947Re: TWinSocketStream Read in BYTE buffer
From: Remy Lebeau (TeamB)
Date: Wed, 28 Dec 2005 08:57
Date: Wed, 28 Dec 2005 08:57
174 lines
6582 bytes
6582 bytes
"Maurice Anderson" <mauriceanderson@hotmail.com> wrote in message
news:43af06d7$1@newsgroups.borland.com...
> How would I do that?
What exactly was unclear about my earlier statement? Allocate a new buffer
that is the same as, or larger than, the sum of the number of bytes of the
other two buffers combined. And then copy the bytes of the two buffers into
the new one.
> 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.
Why aren't you using the code I already gave you? I showed you exactly how
to send the byte size of the data that is being sent. Just expand on that
code to send an additional value in front of the byte size to indicate the
type of data being sent. For example:
// sending a text message ...
{
char type = 0x01; // text
if( WriteBuf(str, &type, sizeof(char)) )
{
if( WriteString(str, "Hello World!") )
// text message sent ok ...
else
// text message error ...
}
else
// text message error ...
}
// sending a file ...
{
// open a file ...
int datasize = ...; // get the file size
char type = 0x02; // file
if( WriteBuf(str, &type, sizeof(char)) )
{
if( WriteString(str, "the filename") )
{
if( WriteBuf(str, &datasize, sizeof(int)) )
{
BYTE rbuf[256];
int bufsize;
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 ...
}
// read a packet and decide how to handle it ...
{
char type = 0x00;
if( ReadBuf(str, &type, sizeof(char)) )
{
switch( type )
{
case 0x01: // text
{
AnsiString msg;
if( ReadString(str, msg) )
// use msg as needed ...
break;
}
case 0x02: // file
{
AnsiString filename;
if( ReadString(str, filename) )
{
BYTE rbuf[256];
int bufsize, datasize = 0;
bool error = false;
if( ReadBuf(str, &datasize, sizeof(int)) )
{
// 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 bufsize
bytes ...
datasize -= bufsize;
}
// close the file ...
if( error )
// delete incomplete file ...
}
}
break;
}
default: // unknown type
{
// unknown data received, ignore it ...
int datasize = 0;
if( ReadBuf(str, &datasize, sizeof(int)) )
{
BYTE rbuf[256];
int bufsize;
while( datasize > 0 )
{
bufsize = datasize;
if( bufsize > sizeof(rbuf) )
bufsize = sizeof(rbuf);
if( !ReadBuf(str, rbuf, bufsize) )
break;
datasize -= bufsize;
}
}
break;
}
}
}
}
> So the 'frame' would look something like this format:
> [type code][data length][file name][data].
There is no point in putting a filename into a text message. But in
general, it is good to frame your packets, yes.
> So my code would look something like this:
No, it would not. Not even close.
Gambit
Technical Details
Message-ID:
<43b252fa$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!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsfeed.inode.at!reader.uucp.at!feeder02.uucp.at!newsfeed.uucp.at!news.uucp.at!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: "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> Subject: Re: TWinSocketStream Read in BYTE buffer Date: Wed, 28 Dec 2005 00:57:30 -0800 Lines: 176 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: <43b252fa$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1135760122 68.69.17.41 (28 Dec 2005 00:55:22 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsfeed.inode.at!reader.uucp.at!feeder02.uucp.at!newsfeed.uucp.at!news.uucp.at!newsgroups.borland.com!not-for-mail