Recent

Author Topic: [SOLVED]How can I send Data using Indy  (Read 3999 times)

Nolan

  • New Member
  • *
  • Posts: 15
[SOLVED]How can I send Data using Indy
« on: August 01, 2019, 03:06:21 pm »
Good afternoon,

I come from a C# angle. I am totally new to Sockets in Delphi (i am studying on my own, no Tutor, just few Youtube videos and some books too) and i have been picking up Pascal for sometime now, and after some research i came across this forum (Its a Lazarus / Free pascal forum, but Object pascal can be looked at here) and i am using the Delphi XE8 IDE with indy to do this.

Now i have seen the IdTCPServer and IdTCPClient now i drag them both to the VCL form and i get lost from there as i do not know how to send messages between the both of them (I am new to this and need clarification)

Somehow , I would be appreciating any form of Help anyone can render as to how to do something like this

Is it here i can send the data to the client? Like so :

Code: Pascal  [Select][+][-]
  1. procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
  2. begin
  3.         // Code to send message to Client
  4. end;

And how does the client tend to recieve and display the information?
« Last Edit: August 02, 2019, 01:55:03 pm by Nolan »


Nolan

  • New Member
  • *
  • Posts: 15
Re: How can I send Data using Indy
« Reply #2 on: August 02, 2019, 01:24:34 pm »
Like this ?

On the server I have this

Server

Code: Pascal  [Select][+][-]
  1. unit Server;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdBaseComponent, IdComponent,
  8.   IdCustomTCPServer, IdTCPServer, IdContext, Vcl.StdCtrls;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     IdTCPServer1: TIdTCPServer;
  13.     Label1: TLabel;
  14.     procedure FormCreate(Sender: TObject);
  15.     procedure IdTCPServer1Execute(AContext: TIdContext);
  16.   private
  17.     { Private declarations }
  18.   public
  19.     { Public declarations }
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1;
  24.  
  25. implementation
  26.  
  27. {$R *.dfm}
  28.  
  29. procedure TForm1.FormCreate(Sender: TObject);
  30. begin
  31. IdTCPServer1:=TIdTCPServer.Create(nil);
  32. IdTCPServer1.DefaultPort:=50000;
  33. IdTCPServer1.OnExecute:=IdTCPServer1Execute;
  34. IdTCPServer1.Active:=true;
  35. end;
  36.  
  37. procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
  38. var
  39.   recv:string;
  40. begin
  41.        recv := AContext.Connection.Socket.ReadLn;
  42.        //Display on Label somehow i do not get to see this part work with Label.Caption
  43. end;
  44.  
  45. end.
  46.  
  47.  

And on the Client I have this:

Client

Code: Pascal  [Select][+][-]
  1. unit Client;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdBaseComponent, IdComponent,
  8.   IdTCPConnection, IdTCPClient;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     IdTCPClient1: TIdTCPClient;
  13.     procedure FormCreate(Sender: TObject);
  14.   private
  15.     { Private declarations }
  16.   public
  17.     { Public declarations }
  18.   end;
  19.  
  20. var
  21.   Form1: TForm1;
  22.  
  23. implementation
  24.  
  25. {$R *.dfm}
  26.  
  27. procedure TForm1.FormCreate(Sender: TObject);
  28. begin
  29. IdTCPClient1:=TIdTCPClient.Create(nil);
  30. IdTCPClient1.Host:='localhost';
  31. IdTCPClient1.Port:=50000;
  32. IdTCPClient1.Connect;
  33. IdTCPClient1.Socket.WriteLn('Hello World..!');
  34. end;
  35.  
  36. end.
  37.  
  38.  

However, one last issue

i want it to show on the label whatever it transfers from server to client on a Label to confirm functionality , i did something like this

Code: Pascal  [Select][+][-]
  1. Label1.Caption = recv;
  2.  

Wanted it to show here

(https://i.postimg.cc/fJHqMGCJ/tartu.png)

And i got an Error instead.

« Last Edit: August 02, 2019, 01:27:11 pm by Nolan »

Nolan

  • New Member
  • *
  • Posts: 15
Re: How can I send Data using Indy
« Reply #3 on: August 02, 2019, 01:51:24 pm »
My mistake, i got it to work now and its working perfectly fine, No worries again.

I was supposed to do something like this :
Code: Pascal  [Select][+][-]
  1. Label1.Caption := recv;

I missed the Semi colon , thinking it was C#
 
Thanks , so let me see how i can expand my skill with this.
Once again. Thanks

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1572
    • Lebeau Software
Re: [SOLVED]How can I send Data using Indy
« Reply #4 on: August 02, 2019, 11:07:31 pm »
Note that TIdTCPServer is a multi-threaded component.  Its OnConnect, OnDisconnect, OnExecute, and OnException events are triggered in the context of worker threads, not in the context of the main UI thread.  As such, you MUST sync with the main UI thread in order to safely access UI controls from within those events.  You can use Indy's TIdSync or TIdNotify classes in the IdSync unit, or the RTL's TThread.Synchronize() or TThread.Queue() methods in the Classes unit.  Or any other inter-thread mechanism you want to use to delegate code execution to the main UI thread (window/thread messages, thread-safe queues, etc).
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018