Article View: borland.public.cppbuilder.internet.socket
Article #5361Re: Problem with sending data with TCS/TSS (data has been received mixed and/or damaged)
From: Vladimir Stefanovic
Date: Fri, 20 Aug 2004 13:44
Date: Fri, 20 Aug 2004 13:44
95 lines
3242 bytes
3242 bytes
> Is that in connection with servers' blocking modes?
Hi Remy,
I think I finally discovered the right way to connect to server
and not to think of the common OnRead().
I just do not know yet the best way to inform the server of
the expecting data size. Can I somehow involve TMemoryStream
in this code and so the final TMemoryStream::Size is the size
I expected?
Ig I have to imnform the server of the stream size separately,
how the server knows to whome is the received size addressed?
In this code I explicitely said that I want 44800 bytes.
[code is based on your samples in these ng-s]
void __fastcall TsrvForm::ServerSocketGetThread(TObject *Sender,
TServerClientWinSocket *ClientSocket,
TServerClientThread *&SocketThread)
{
SocketThread = new TLanServerThread( false, ClientSocket );
}
void __fastcall TLanServerThread::ClientExecute()
{
char *SomeBuffer = new char[44800];
int BufferSize = 44800;
TWinSocketStream *Stream = new TWinSocketStream ( ClientSocket,
5000 );
try
{
while( ( !Terminated ) && ( ClientSocket->Connected ) )
{
if ( Stream->WaitForData ( 5000 ) )
{
if( !ReadData ( Stream, SomeBuffer, BufferSize ) )
break;
// use your buffered data as needed...
FILE *p;
AnsiString File = ExtractFilePath( ParamStr(0) ) +
"Copy.txt";
p = fopen( File.c_str(), "wb+" );
fwrite ( SomeBuffer, 1, 44800, p );
fclose (p);
}
}
}
__finally
{
delete Stream;
delete SomeBuffer;
}
}
bool __fastcall TLanServerThread::ReadData( TWinSocketStream *Stream,
void *Buffer, int BufferSize )
{
LPBYTE Buf = (LPBYTE)Buffer;
int NumRead, TotalRead = 0;
try
{
while ( TotalRead < BufferSize )
{
NumRead = Stream->Read( &Buf[TotalRead],
BufferSize-TotalRead );
if( NumRead > 0 )
TotalRead += NumRead;
else
return false;
}
}
catch ( const Exception& )
{
return false;
}
return true;
}
Best regards,
Vladimir Stefanovic
Technical Details
Message-ID:
<41260071@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<41245031$1@newsgroups.borland.com> <4124d7ab@newsgroups.borland.com> <4124f2ca@newsgroups.borland.com> <4125baa0$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,9d2d5d657ea2710a X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Vladimir Stefanovic" <spam@not.needed> Newsgroups: borland.public.cppbuilder.internet.socket References: <41245031$1@newsgroups.borland.com> <4124d7ab@newsgroups.borland.com> <4124f2ca@newsgroups.borland.com> <4125baa0$1@newsgroups.borland.com> Subject: Re: Problem with sending data with TCS/TSS (data has been received mixed and/or damaged) Date: Fri, 20 Aug 2004 15:44:52 +0200 Lines: 98 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2314.1300 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 NNTP-Posting-Host: 213.137.116.147 Message-ID: <41260071@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1093009521 213.137.116.147 (20 Aug 2004 06:45:21 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail