SolodovRD wrote:I need to send some message
- Code: Select all
IdTCPServer1->Connection->IOHandler->Write(Message.Length());
IdTCPServer1->Connection->IOHandler->Write(Message);
And which connection are you expecting that to write to? Like TServerSocket, TTcpServer, and jst about any other TCP server component, TIdTCPServer can have multiple connections active at one time. You have to decide which client you want to send to, and then use that client's Connection->IOHandler object. The connections are available in the TIdTCPServer::Contexts property, eg:
- Code: Select all
TList *list = IdTCPServer1->Contexts->LockList();
try
{
TIdContext *ctx = (TIdContext*) list->Items[index of the desired client here];
ctx->Connection->IOHandler->Write(Message.Length());
ctx->Connection->IOHandler->Write(Message);
}
__finally
{
IdTCPServer1->Contexts->UnlockList();
}
That being said, TIdTCPServer is a multi-threaded component (each client runs in its own thread), so you really should be doing all of the writing in the TIdTCPServer::OnExecute() event instead:
- Code: Select all
void __fastcall TMyForm::IdTCPServer1Execute(TIdContext *AContext)
{
...
String Message = ...;
AContext->Connection->IOHandler->Write(Message.Length());
AContext->Connection->IOHandler->Write(Message);
...
}
If you need to send messages from your main UI thread (or any other thread, for that matter), you should give each client its own thread-safe queue of outgoing messages, and then you can push messages into that queue when needed, and have OnExecute() poll messages from that queue periodically. I have demonstrated that technique many many times before in many different forums.
SolodovRD wrote:So, Anyway IdTCPServer not include such method as Write or Writeln
Yes it does, you are just not looking for it in the right place.
SolodovRD wrote:that I seen in several samples in internet. Also it Have no "Connection" method, I found IOHandler, but directly in a IdTCPServer
Like this
- Code: Select all
Form1->IdTCPServer1->IOHandler->
That is a different type of IOHandler than you are looking for. You cannot send/receive data using the server's own IOHandler. Its purpose is only to accept client connections. You have to use each client's own IOHandler to exchange data with the clients.
SolodovRD wrote:Also Server and Client have no any method for receiving strings I seen in a samples
Again, yes it does, you are just not looking for it in the right place. You need to use the IOHandler that belongs to the provided TIdContext object:
- Code: Select all
class TMemoAddNotify : public TIdNotify
{
private:
String FMsg;
__fastcall TMemoAddNotify(const String &AMsg)
: TIdNotify(), FMsg(AMsg)
{
}
protected:
void __fastcall DoNotify()
{
Form1->Memo1->Lines->Add(FMsg);
}
public:
static void __fastcall AddToMemo(const String &AMsg)
{
(new TMemoAddNotify(AMsg))->Notify();
}
};
void __fastcall TForm1::IdTCPServer1Execute(TIdContext *AContext)
{
String S = AContext->Connection->IOHandler->ReadLn(); // or ReadString(), or whatever other Read...() method you need...
// NOTE: because this event is running in a worker thread, you MUST
// sync with the main UI thread in order to access UI controls safely...
//Memo1->Lines->Add(S);
TMemoAddNotify::AddToMemo(S);
}