Forum > General

How to get message from Server to Client. tIdTCPServer to TIdTCPClient [SOLVED]

<< < (2/3) > >>

Nolan:

--- Quote from: MarkMLl on August 06, 2022, 11:30:22 pm ---Why are you asking in this forum when you're using Delphi? There's almost certainly more appropriate places... as you should have realised by now, since you've been doing the same thing for months.

Try recompiling with FPC/Lazarus and more of us might be interested... :-)

MarkMLl

--- End quote ---

Ahh, I see, Let me download Lazarus then. Thanks for the heads up.

MarkMLl:

--- Quote from: Nolan on August 06, 2022, 11:43:02 pm ---Ahh, I see, Let me download Lazarus then. Thanks for the heads up.

--- End quote ---

Well taken :-) It was your usage of "vcl." that I spotted, you might find minor differences in Lazarus/LCL's handling of things but all in all a high priority is Delphi compatibility... as far as is possible within the constraint of portability to a wide range of target OSes etc.

MarkMLl

Remy Lebeau:

--- Quote from: MarkMLl on August 06, 2022, 11:30:22 pm ---Why are you asking in this forum when you're using Delphi? There's almost certainly more appropriate places... as you should have realised by now, since you've been doing the same thing for months.

--- End quote ---

Official Indy forum: https://atozed.com/forums/forum-9.html

Also see: https://www.indyproject.org/support/

Remy Lebeau:

--- Quote from: Nolan on August 06, 2022, 06:51:24 pm ---Now the Client sends a command to the server to execute a function (This works good) now, in the same vein, i want a scenario where it returns something like 'Done' once the server has executed the program and it should show on the Label on the Client that everything is fine.

--- End quote ---

The simplest solution is to just call ReadLn() after WriteLn(), eg:


--- Quote from: Nolan on August 06, 2022, 06:51:24 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm3.Button1Click(Sender: TObject);begin  IdTCPClient1.Socket.WriteLn('SayHello');  Label1.Caption := IdTCPClient1.Socket.ReadLn;end;
--- End quote ---


--- Quote from: Nolan on August 06, 2022, 06:51:24 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure SayHelloWorldMessageBox();begin  ShowMessage('Hello World!');end;
--- End quote ---

Note that TIdTCPServer is multi-threaded, where the OnExecute event is fired in worker threads, but ShowMessage() is not a thread-safe function. It should never be called outside of the main UI thread.  So, either use TThread.Synchronize() or equivalent to call ShowMessage() in the main UI thread, or else use a thread-safe function, such as Windows.MessageBox().


--- Quote from: Nolan on August 06, 2022, 06:51:24 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---AContext.Connection.IOHandler.Write('Done');
--- End quote ---

You should call Socket.WriteLn() instead of Socket.Write(), otherwise the client won't know when the string ends, unless you close the connection (which you are not doing) after sending the string.


--- Quote from: Nolan on August 06, 2022, 06:51:24 pm ---Why does it not send a message from the Server to the Client to Display that "done",

--- End quote ---

It does (assuming ShowMessage() does not deadlock/crash the calling thread), but the client you have shown is not coded to read the message.

Nolan:

--- Quote from: Remy Lebeau on August 08, 2022, 09:15:24 pm ---
--- Quote from: Nolan on August 06, 2022, 06:51:24 pm ---Now the Client sends a command to the server to execute a function (This works good) now, in the same vein, i want a scenario where it returns something like 'Done' once the server has executed the program and it should show on the Label on the Client that everything is fine.

--- End quote ---

The simplest solution is to just call ReadLn() after WriteLn(), eg:


--- Quote from: Nolan on August 06, 2022, 06:51:24 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm3.Button1Click(Sender: TObject);begin  IdTCPClient1.Socket.WriteLn('SayHello');  Label1.Caption := IdTCPClient1.Socket.ReadLn;end;
--- End quote ---


--- Quote from: Nolan on August 06, 2022, 06:51:24 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure SayHelloWorldMessageBox();begin  ShowMessage('Hello World!');end;
--- End quote ---

Note that TIdTCPServer is multi-threaded, where the OnExecute event is fired in worker threads, but ShowMessage() is not a thread-safe function. It should never be called outside of the main UI thread.  So, either use TThread.Synchronize() or equivalent to call ShowMessage() in the main UI thread, or else use a thread-safe function, such as Windows.MessageBox().


--- Quote from: Nolan on August 06, 2022, 06:51:24 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---AContext.Connection.IOHandler.Write('Done');
--- End quote ---

You should call Socket.WriteLn() instead of Socket.Write(), otherwise the client won't know when the string ends, unless you close the connection (which you are not doing) after sending the string.


--- Quote from: Nolan on August 06, 2022, 06:51:24 pm ---Why does it not send a message from the Server to the Client to Display that "done",

--- End quote ---

It does (assuming ShowMessage() does not deadlock/crash the calling thread), but the client you have shown is not coded to read the message.

--- End quote ---

You are a Blessing, Thank You so much! Tried all you did and it responded fine! Now i can do a two way app.
Thank you. Will try to see how to code a VNC now!

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version