Recent

Author Topic: TCP Server in Android  (Read 744 times)

zakidrus

  • Newbie
  • Posts: 3
TCP Server in Android
« 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.

kordal

  • New Member
  • *
  • Posts: 17
Re: TCP Server in Android
« Reply #1 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.
« Last Edit: November 21, 2019, 02:18:39 am by kordal »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 680
    • Lebeau Software
Re: TCP Server in Android
« Reply #2 on: November 22, 2019, 03:55:30 am »
Indy works in FreePascal and has TCP client/server components that work on Android.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

zakidrus

  • Newbie
  • Posts: 3
Re: TCP Server in Android
« Reply #3 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.  

lucamar

  • Hero Member
  • *****
  • Posts: 2144
Re: TCP Server in Android
« Reply #4 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;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.4/2.0.6  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

kordal

  • New Member
  • *
  • Posts: 17
Re: TCP Server in Android
« Reply #5 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.

zeljkoc

  • Full Member
  • ***
  • Posts: 101
    • Zeljko Cvijanovic
Re: TCP Server in Android
« Reply #6 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

zakidrus

  • Newbie
  • Posts: 3
Re: TCP Server in Android
« Reply #7 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

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 680
    • Lebeau Software
Re: TCP Server in Android
« Reply #8 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.
« Last Edit: November 26, 2019, 03:58:32 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)