Lazarus

Programming => Operating Systems => Android => Topic started by: zakidrus on November 19, 2019, 12:11:30 am

Title: TCP Server in Android
Post by: zakidrus on November 19, 2019, 12:11:30 am
Hi Everyone

I'am newbie in Laz4android. What I have done so far are installing the Laz4android, LAMW, jdk, sdk, ndk, compilling my first "hello world" apk, and run it in android phone. Those all :D . But further, I need make an apk that will work as TCP Server (listening tcp). I have done it in PC (windows) before and I believe it's possible in android as well. What is the easiest way to do that? Any component or code that work in the purpose?

Many thanks.
Title: Re: TCP Server in Android
Post by: kordal on November 21, 2019, 01:29:43 am
Hi. LAMW currently only has JUDPSocket and JTCPSocketClient components. There is no TCP server in this implementation. Decision:

1. Use JAVA and class ServerSocket.
2. Use RAD Studio 10.x and Indy or other components.
Title: Re: TCP Server in Android
Post by: Remy Lebeau on November 22, 2019, 03:55:30 am
Indy works in FreePascal and has TCP client/server components that work on Android.
Title: Re: TCP Server in Android
Post by: zakidrus on November 23, 2019, 02:57:48 pm
Thank You Remy Lebeau for The Indy Component.

Finally, I am able to put Indy on Laz4Android. But I found problem when running the app. It will close directly whenever I open the TCP port.

This is my code for creating and open the port. Button 1 is triggering the app to close.

Code: Pascal  [Select]
  1. procedure TAndroidModule1.AndroidModule1Create(Sender: TObject);
  2. begin
  3.   IdTCPServer1                   := TIdTCPServer.create;
  4.   IdTCPServer1.OnConnect         := IdTCPServer1Connect;
  5.   IdTCPServer1.OnDisconnect      := IdTCPServer1Disconnect;
  6.   IdTCPServer1.OnException       := IdTCPServer1Exception;
  7.   IdTCPServer1.OnExecute         := IdTCPServer1Execute;
  8. end;
  9.  
  10. procedure TAndroidModule1.jButton1Click(Sender: TObject);
  11. begin
  12.   IdTCPServer1.Bindings.Add.Port := 6000;
  13.   IdTCPServer1.Active            := True;
  14. end;
  15.  
Title: Re: TCP Server in Android
Post by: lucamar on November 23, 2019, 03:25:18 pm
If your mode is OBJFPC (i.e. your code has a {$mode objfpc} directive) try this:

Code: Pascal  [Select]
  1. procedure TAndroidModule1.AndroidModule1Create(Sender: TObject);
  2. begin
  3.   IdTCPServer1                   := TIdTCPServer.create;
  4.   IdTCPServer1.OnConnect         := @IdTCPServer1Connect;
  5.   IdTCPServer1.OnDisconnect      := @IdTCPServer1Disconnect;
  6.   IdTCPServer1.OnException       := @IdTCPServer1Exception;
  7.   IdTCPServer1.OnExecute         := @IdTCPServer1Execute;
  8. end;
Title: Re: TCP Server in Android
Post by: kordal on November 24, 2019, 02:55:56 am
Working with sockets usually requires a separate thread. As I understand it, this is especially critical in Android OS. The IdTCPServerExecute method should not directly work with the GUI of the main thread and use global variables. To do this, you need a synchronization mechanism, for example, critical sections. How Indy works in Android on FPC never tested.
Title: Re: TCP Server in Android
Post by: zeljkoc on November 24, 2019, 08:06:50 am
I use indie for android
(indy-10.2.0.3.zip)

https://forum.lazarus.freepascal.org/index.php/topic,18958.msg107445.html#msg107445

with android i transfer data to firebird server, with indy
Title: Re: TCP Server in Android
Post by: zakidrus on November 24, 2019, 01:28:42 pm
Thanks Lucamar for the advice.
I tried OBJFPC mode and use the directive @ but the problem still there. App close when I open the port. I am start thinking it's caused by Android security although I have checked all permission needed by the app
Title: Re: TCP Server in Android
Post by: Remy Lebeau on November 26, 2019, 03:53:46 am
Finally, I am able to put Indy on Laz4Android. But I found problem when running the app. It will close directly whenever I open the TCP port.

Then the TIdTCPServer.Active property setter is likely raising an exception that you are not catching.

Working with sockets usually requires a separate thread. As I understand it, this is especially critical in Android OS.

TIdTCPServer is a multi-threaded component.  It create a new worker thread for each entry in the TIdTCPServer.Bindings property, and then calls the OnConnect, OnExecute, OnDisconnect, and OnException events in the context of those worker threads.

I use indie for android
(indy-10.2.0.3.zip)

That is an extremely old version of Indy.  The current version is 10.6.2.  Indy is available in FreePascal's Online Package Manager (https://wiki.freepascal.org/Online_Package_Manager).