Article View: borland.public.cppbuilder.internet.socket
Article #4485Re: Send buffer from server to a client
From: Remy Lebeau (TeamB)
Date: Thu, 27 Jan 2005 18:59
Date: Thu, 27 Jan 2005 18:59
124 lines
4547 bytes
4547 bytes
"Vin๏ฟฝcius" <vgalo2000@yahoo.com.br> wrote in message
news:41f90c54@newsgroups.borland.com...
> int tamanho = Edit1->Text.Length();
> IdTCPClient1->WriteBuffer(Edit1->Text.c_str(), tamanho);
You can simplify that to the following:
IdTCPClient1->Write(Edit1->Text);
> TIdBuffer * buffer = new TIdBuffer;
You don't need that. You are not using it anyway.
> int tamanho = AThread->Connection->ReadFromStack();
> char * bufferread = new char[tamanho];
> AThread->Connection->ReadBuffer(bufferread, tamanho);
You can simplify that to the following:
AnsiString buffer = AThread->Connection->CurrentReadBuffer();
> ListBox1->Items->Add((AnsiString)bufferread);
You cannot safely do that. The event handlers in Indy server have an
AThread parameter. The handlers are triggered in the context of the
specified worker thread, not the main VCL thread. The VCL is not
thread-safe. To safely access the GUI from a worker thread, you need to use
the thread's Synchronize() method, either directly or via a TIdSync
descendant class. For example:
#include <memory>
class TUpdateListBox : public TIdSync
{
protected:
TListBox *FListBox;
AnsiString FStr;
void __fastcall DoSynchronize();
public:
__fastcall TUpdateListBox(TIdThread *AThread, TListBox *AListBox,
const AnsiString &AStr);
static void __fastcall Add(TIdThread *AThread, TListBox *AListBox,
const AnsiString &AStr);
};
__fastcall TUpdateListBox::TUpdateListBox(TIdThread *AThread, TListBox
*AListBox, const AnsiString &AStr)
: TIdSync(AThread), FListBox(AListBox), FStr(AStr)
{
}
void __fastcall TUpdateListBox::DoSynchronize()
{
FListBox->Items->Add(FStr);
}
void __fastcall TUpdateListBox::Add(TIdThread *AThread, TListBox
*AListBox, const AnsiString &AStr)
{
std::auto_ptr<TUpdateListBox> ULB(new TUpdateListBox(AThread,
AListBox, AStr));
ULB->Synchronize();
}
void __fastcall TForm1::IdTCPServer1Execute(TIdPeerThread *AThread)
{
AnsiString buffer = AThread->Connection->CurrentReadBuffer();
TUpdateListBox::Add(AThread, ListBox1, buffer);
}
> But the client don't have this multithread functionality, and don't
> have the "Execute" How can I do something similar, send buffers
> to client and read them??
If you want the client to receive data asynchronously, then you have to do
the same thing that the server does - run a separate thread that
continuously reads from the socket. For example:
class TMyReadThread : public TThread
{
private:
TIdTCPConnection *FConn;
protected:
void __fastcall Execute();
public:
__fastcall TMyReadThread(TIdTCPConnection *AConn);
};
__fastcall TMyReadThread::TMyReadThread(TIdTCPConnection *AConn)
: TThread(true), FConn(AConn)
{
Resume();
}
void __fastcall TMyReadThread::Execute()
{
try
{
while( !Terminated )
{
AnsiString buffer = FConn->CurrentReadBuffer();
// use buffer as needed...
}
}
catch(const Exception &) {
return;
}
}
IdTCPClient1->Connect(1000);
TMyReadThread * thread = new TMyReadThread(IdTCPClient1);
//...
thread->Terminate();
IdTCPClient1->Disconnect();
thread->WaitFor();
delete thread;
Gambit
Technical Details
Message-ID:
<41f93966$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.com!news.glorb.com!proxad.net!proxad.net!194.117.148.138.MISMATCH!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<41f90c54@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,2b30b06ce9be7f64 X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.internet.socket References: <41f90c54@newsgroups.borland.com> Subject: Re: Send buffer from server to a client Date: Thu, 27 Jan 2005 10:59:21 -0800 Lines: 126 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: <41f93966$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1106852198 68.69.17.41 (27 Jan 2005 10:56:38 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.com!news.glorb.com!proxad.net!proxad.net!194.117.148.138.MISMATCH!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail