Recent

Author Topic: TClientSocket and TServerSocket  (Read 15083 times)

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
TClientSocket and TServerSocket
« on: January 07, 2019, 06:26:07 pm »
Hi,

I know that Delphi contains these components: TClientSocket and TServerSocket.
Unfortunately I could not find them in Lazarus.
Is it possible to get them somehow, or any workaround to substitute them?
Thank you

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: TClientSocket and TServerSocket
« Reply #1 on: January 07, 2019, 06:55:23 pm »
IIRC there are (or were?) TClientSocket and TServerSocket implementations in Free Pascal but there are other options. Search in the wiki for "Networking" to find them.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: TClientSocket and TServerSocket
« Reply #2 on: January 07, 2019, 07:20:13 pm »
I don't know what IIRC is, but I know I cannot find it.
I am just a newbie and don't know anything about the sockets, but found this article: https://www.thoughtco.com/write-network-aware-applications-with-delphi-4071210
So, I would need TClientSocket and TServerSocket to be able to do similar connections.
Any help how should I do this?

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: TClientSocket and TServerSocket
« Reply #3 on: January 07, 2019, 07:21:49 pm »
https://acronyms.thefreedictionary.com/IIRC

Check out if OPM (Online Package Manager) does have something similar to offer for you. I would personally suggest Indy Internet Tools.
« Last Edit: January 07, 2019, 07:24:25 pm by Cyrax »

balazsszekely

  • Guest
Re: TClientSocket and TServerSocket
« Reply #4 on: January 07, 2019, 07:37:48 pm »
Indy, Synapse, Lnet, ICS, etc...just to mention a few.

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: TClientSocket and TServerSocket
« Reply #5 on: January 07, 2019, 07:42:19 pm »
Indy, Synapse, Lnet, ICS, etc...just to mention a few.
Guys, I need for TClientSocket and TServerSocket.
Is there something that is exactly the same as I am looking for? Or these are just similar tools? If just similar, which is the closest one for my need?

balazsszekely

  • Guest
Re: TClientSocket and TServerSocket
« Reply #6 on: January 07, 2019, 08:11:25 pm »
@justnewbie
Quote
Is there something that is exactly the same as I am looking for?
No.

Quote
Or these are just similar tools?
Yes, just similar tools.

Quote
If just similar, which is the closest one for my need?
I would say synapse. You can find many client/server example out there. What exactly are you trying to achieve?


justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: TClientSocket and TServerSocket
« Reply #7 on: January 07, 2019, 08:14:46 pm »
I installed the Indy package, I got tons of new tabs and tons of new components under every new tab.
To be honest it is not really helpful.
I want to make a server-client communication like in this Delphi article: https://www.thoughtco.com/write-network-aware-applications-with-delphi-4071210
I don't know sockets, but this example is pretty good and easy to start with. Unfortunately it is under Delphi. This is why I need something very similar.

Edit: it is really confusing. I removed the Indy package and installed the Synapse. But, cannot see any new components. Does not Synapse have any component?
Edit2: OK, I found this about Synapse: "The Project contains simple low level non-visual objects for easiest programming without problems."

« Last Edit: January 07, 2019, 08:27:39 pm by justnewbie »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: TClientSocket and TServerSocket
« Reply #8 on: January 07, 2019, 08:22:37 pm »
Guys, I need for TClientSocket and TServerSocket.
Is there something that is exactly the same as I am looking for?

No, there isn't.

Quote
Or these are just similar tools? If just similar, which is the closest one for my need?

I may be wrong but I think Synapse have low-level socket classes similar to what you want. It's also worth mentioning that similar classes are already inside the fcl-net package and you will find examples of their use in both the [fpc_source]/packages/fcl-net/examples/ and [fpc_source]/packages/fcl-base/examples/.

Please, search the wiki. Not all is up-to-date but you'll at least find hints and pointers to some good answers.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: TClientSocket and TServerSocket
« Reply #9 on: January 07, 2019, 08:31:16 pm »
Thank you, I will try to start with them, but hoped that there is something similar in its simplicity than the Delphi solution.
It's a pity that TClientSocket and TServerSocket don't exist in Lazarus. For a newbie like me it is a little bit difficult.
« Last Edit: January 07, 2019, 08:43:34 pm by justnewbie »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: TClientSocket and TServerSocket
« Reply #10 on: January 07, 2019, 09:28:21 pm »
I installed the Indy package, I got tons of new tabs and tons of new components under every new tab.
To be honest it is not really helpful.
I want to make a server-client communication like in this Delphi article: https://www.thoughtco.com/write-network-aware-applications-with-delphi-4071210

You can use Indy's TIdTCPClient and TIdTCPServer component for that.  You just have to take into account that Indy uses blocking sockets exclusively, whereas Delphi's TClientSocket/TServerSocket use non-blocking sockets by default (but can also use blocking sockets).  It is best not to perform blocking operations in the main UI thread (Indy servers are multi-threaded), so typically you should perform your client actions in a separate worker thread so as not to cause blockages in the UI (but, for purposes of this example, I'll skip that. You can put a TIdAntiFreeze component on your main Form instead).

I don't know sockets, but this example is pretty good and easy to start with. Unfortunately it is under Delphi. This is why I need something very similar.

A direct translation of that article's example would look like the following in Indy:

Server:

Code: Pascal  [Select][+][-]
  1. interface
  2.  
  3. uses
  4.   ..., IdContext, IdTCPServer;
  5.  
  6. type
  7.   TForm1 = class(TForm)
  8.     IdTCPServer1: TIdTCPServer;
  9.     procedure FormCreate(Sender: TObject);
  10.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  11.     procedure IdTCPServer1Execute(AContext : TIdContext);
  12.   ...
  13.   end;
  14.  
  15. ...
  16.  
  17. implementation
  18.  
  19. uses
  20.   IdSync;
  21.  
  22. procedure TForm1.FormCreate(Sender: TObject);
  23. begin
  24.   IdTCPServer1.DefaultPort := 23;
  25.   IdTCPServer1.Active := True;
  26. end;
  27.  
  28. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  29. begin
  30.   IdTCPServer1.Active := False;
  31. end;
  32.  
  33. // TIdTCPServer is a multi-threaded component, so you MUST sync with
  34. // the main thread when accessing UI controls.  Indy provides two utility
  35. // classes for this purpose - TIdSync (synchronous) and TIdNotify (asynchronous).
  36. // Or, you can use whatever inter-thread sync mechanism you want, as
  37. // long as it is thread-safe.  Opt for asynchronous when possible, to avoid
  38. // blocking the server if client data arrives during shutdown...
  39. type
  40.   TAddToMemo = class(TIdNotify)
  41.   private
  42.     FPeerIP, FMsg: string;
  43.   protected
  44.     procedure DoNotify; override;
  45.   public
  46.     constructor Create(const APeerIP, AMsg: string); reintroduce;
  47.   end;
  48.  
  49. constructor TAddToMemo.Create(const APeerIP, AMsg: string);
  50. begin
  51.   inherited Create;
  52.   FPeerIP := APeerIP;
  53.   FMsg := AMsg;
  54. end;
  55.  
  56. procedure TAddToMemo.DoNotify;
  57. begin
  58.   Form1.Memo1.Lines.Add(FPeerIP + ' sends :') ;
  59.   Form1.Memo1.Lines.Add(FMsg);
  60. end;
  61.  
  62. procedure TForm1.IdTCPServer1Execute(AContext : TIdContext);
  63. var
  64.   sRec: string;
  65. begin
  66.   // check if there is any data pending
  67.   AContext.Connection.IOHandler.CheckForDataOnSource(100);
  68.   AContext.Connection.IOHandler.CheckForDisconnect;
  69.   if AContext.Connection.IOHandler.InputBufferIsEmpty then Exit;
  70.  
  71.   // read all pending data as a single string
  72.   sRec := AContext.Connection.IOHandler.InputBufferAsString;
  73.  
  74.   // add data to the UI
  75.   TAddToMemo.Create(AContext.Binding.PeerIP, sRec).Notify;
  76. end;
  77.  

Client:

Code: Pascal  [Select][+][-]
  1. interface
  2.  
  3. uses
  4.    ..., IdTCPClient;
  5.  
  6. type
  7.   TForm1 = class(TForm1)
  8.     IdTCPClient1 := TIdTCPClient;
  9.     procedure FormCreate(Sender: TObject);
  10.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  11.     procedure Button1Click(Sender: TObject);
  12.   ...
  13.   end;
  14.  
  15. ...
  16.  
  17. implementation
  18.  
  19. procedure TForm1.FormCreate(Sender: TObject);
  20. begin
  21.   IdTCPClient1.Port := 23;
  22.   //local TCP/IP address of the server
  23.   IdTCPClient1.Host := '192.168.167.12';
  24.   IdTCPClient1.Connect;
  25. end;
  26.  
  27. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  28. begin
  29.   IdTCPClient1.Disconnect;
  30. end;
  31.  
  32. procedure TForm1.Button1Click(Sender: TObject);
  33. begin
  34.   if IdTCPClient1.Connected then
  35.     IdTCPClient1.IOHandler.Write(Edit1.Text);
  36. end;
  37.  

Though, a slightly more efficient example would look like this instead:

Server:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.IdTCPServer1Execute(AContext : TIdContext);
  2. var
  3.   sRec: string;
  4. begin
  5.   sRec := AContext.Connection.IOHandler.ReadLn;
  6.   TAddToMemo.Create(AContext.Binding.PeerIP, sRec).Notify;
  7. end;
  8.  

Client:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   if IdTCPClient1.Connected then
  4.     IdTCPClient1.IOHandler.WriteLn(Edit1.Text);
  5. end;
  6.  

Edit: it is really confusing. I removed the Indy package and installed the Synapse. But, cannot see any new components. Does not Synapse have any component?

No, it does not.  It has utility classes that you have to instantiate in code when needed.
« Last Edit: January 07, 2019, 09:39:21 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: TClientSocket and TServerSocket
« Reply #11 on: January 07, 2019, 09:56:43 pm »
@Remy Lebeau:
First of all, a BIG thank you for this answer! I will study this example you gave, maybe my knowledge will be enough to understand it slowly, step-by-step.

« Last Edit: January 07, 2019, 10:24:27 pm by justnewbie »

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: TClientSocket and TServerSocket
« Reply #12 on: January 08, 2019, 05:19:46 pm »
Newbie question: I tried to run the program (server) provided by Remy, but got this error message:
"unit1.pas(33,3) Fatal: Cannot find IdSync used by Unit1 of the Project Inspector."
How should I resolve this?
(Of course, Indy is installed.)
« Last Edit: January 08, 2019, 06:35:31 pm by justnewbie »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: TClientSocket and TServerSocket
« Reply #14 on: January 08, 2019, 08:09:34 pm »
Thank you Remy. I added the IdSync to the indylaz package, recompiled successfully.
But, when I tried to run the program, I got these errors:
EIdSocketError
EIdCouldNotBindSocket
« Last Edit: January 10, 2019, 01:02:21 pm by justnewbie »

 

TinyPortal © 2005-2018