Article View: borland.public.cppbuilder.internet.socket
Article #5262Re: TClientSocket/TServerSocket data exchange...
From: Vladimir Stefanovic
Date: Mon, 27 Sep 2004 20:25
Date: Mon, 27 Sep 2004 20:25
160 lines
4937 bytes
4937 bytes
Hans Galema wrote
> > I want my Client to send a request to Server to get
> > some file. Imaginary protocol:
> >
> > GET filename.txt
>
> Allow rigth for binary files too:
>
> GET filename.pdf
In fact the files I have to GET are only INI files but it's not
bad to allow also binaries (it costs nothing)
> > Then, I want the Server to receive the request and to
> > send back the Client the desired file contents. Imaginary
> > protocol:
> >
> > FIL this_is_the_pure_ascii_contents_of_the_filename.txt
> Better:
> size FIL this_is_the_pure_contents_of_filename.doc
>
> or
>
> FIL size this_is_the_pure_contents_of_the_filename.doc
>
> The last will do if the client only expects 3 char commands.
Yes, you are correct. I have forgotten that it's easier to send the
size of the file immediatelly.
> > The Server receives data through it's own thread
> > stThreadBlocking & ServerSocketGetThread().
> >
> > How I'm supposed to SEND BACK the Client the desired
> > file? Where I have to place the sending code? Should I
> > have the alternative TClientSocket on Server to use for
> > sending? There are some examples of how to send a row
> > of text back ( SendText() ) but my file can be several KB
> > so I cannot send the file back from only one pass...
>
> Nothing of that all.
>
> You got data from a client using something like
>
> pStream->Read()
>
> in your Threads Execute() method. In the same method you can use
>
> pStream->Write()
>
> to send data to the client.
This is how I receive the data (fits to my post above which sends
data).
Now I have enough material to start.
void __fastcall TsrvForm::ServerSocketGetThread(TObject *Sender,
TServerClientWinSocket *ClientSocket,
TServerClientThread *&SocketThread)
{
SocketThread = new TLanServerThread( false, ClientSocket );
}
void __fastcall TLanServerThread::ClientExecute()
{
TMemoryStream *InboxMemoryStream = new TMemoryStream;
InboxMemoryStream->Position = 0;
int NewSize, OldSize = 0;
TWinSocketStream *Stream = new TWinSocketStream ( ClientSocket,
5000 );
try
{
while( ( !Terminated ) && ( ClientSocket->Connected ) )
{
if ( Stream->WaitForData ( 5000 ) )
{
int BufferSize = 0;
if( !ReadData ( Stream, &BufferSize, sizeof(int) ) )
break;
char *SomeBuffer = new char[ BufferSize ];
try
{
if( !ReadData ( Stream, SomeBuffer, BufferSize ) )
break;
// read how many bytes have already been stored into
buffer...
OldSize = InboxMemoryStream->Size;
// calculate the new size that has to be into buffer
after receiving...
NewSize = OldSize + BufferSize;
// increse the size of the buffer to allow the new
storage...
InboxMemoryStream->Size = NewSize;
// write the just read data into memory stream ...
InboxMemoryStream->WriteBuffer( SomeBuffer,
BufferSize );
}
__finally
{
delete [] SomeBuffer;
}
}
}
InboxMemoryStream->SaveToFile( ExtractFilePath( ParamStr(0) ) +
"Copy.txt" );
}
__finally
{
delete Stream;
delete InboxMemoryStream;
}
}
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:
<415876da@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!news.zanker.org!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<41585538@newsgroups.borland.com> <41586f09@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,20726cd0f74fe942 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: <41585538@newsgroups.borland.com> <41586f09@newsgroups.borland.com> Subject: Re: TClientSocket/TServerSocket data exchange... Date: Mon, 27 Sep 2004 22:25:15 +0200 Lines: 163 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.181 Message-ID: <415876da@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1096316634 213.137.116.181 (27 Sep 2004 13:23:54 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!news.zanker.org!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail