Recent

Author Topic: TCP Server in Android  (Read 5349 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: 20
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: 1312
    • 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: 4219
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/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.

kordal

  • New Member
  • *
  • Posts: 20
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: 145
    • 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: 1312
    • 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 Lazarus' Online Package Manager.
« Last Edit: March 10, 2020, 01:25:16 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

zeljkoc

  • Full Member
  • ***
  • Posts: 145
    • Zeljko Cvijanovic
Re: TCP Server in Android
« Reply #9 on: March 07, 2020, 07:31:37 pm »
Please send link indy 10.6.2 for lazarus

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: TCP Server in Android
« Reply #10 on: March 10, 2020, 01:24:36 am »
Please send link indy 10.6.2 for lazarus

As I already stated, Indy is available in Lazarus' Online Package Manager.  You can install Indy directly from the OPM.

Although, I don't know if the version of Indy in OPM is the absolute latest version available, since Indy migrated from SVN to GitHub a few month ago, but it should be fairly recent.
« Last Edit: March 10, 2020, 01:28:16 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

balazsszekely

  • Guest
Re: TCP Server in Android
« Reply #11 on: March 10, 2020, 06:49:00 am »
@Remy Lebeau
Quote
Although, I don't know if the version of Indy in OPM is the absolute latest version available, since Indy migrated from SVN to GitHub a few month ago, but it should be fairly recent.
I regularly update Indy, so usually is fairly recent. I have a question though. Before switching to git, the version number inside the lpk file looked like this: 10.6.2.XXXX where XXXX was the last SVN revision number. Since git doesn't have a revision number equivalent, what should I put instead of XXXX? For now I went with 10.6.2.0, but perhaps there is a better way to describe the current version in OPM. 
« Last Edit: March 10, 2020, 06:51:41 am by GetMem »

zeljkoc

  • Full Member
  • ***
  • Posts: 145
    • Zeljko Cvijanovic
Re: TCP Server in Android
« Reply #12 on: March 10, 2020, 11:48:40 am »

i tried to install and use for android

Error:
IdGlobal.pas(553,29) Fatal: Cannot find iconvenc used by IdGlobal

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: TCP Server in Android
« Reply #13 on: March 11, 2020, 07:03:24 pm »
Before switching to git, the version number inside the lpk file looked like this: 10.6.2.XXXX where XXXX was the last SVN revision number. Since git doesn't have a revision number equivalent, what should I put instead of XXXX?

Yes, versioning is kind of a mess now that Indy is on GitHub.  The 10.6.2 portion is static (IdVers.inc), but the XXXX was dynamic, based on SVN revision number, as you said.  Actually, GitHub does have an SVN interface, which does have a revision number.  I don't use SVN when checking in Indy's code to GitHub, so I don't really know where that revision number stands, but I do know it is functional.

In Indy 11, we might switch over to semantic versioning instead.  Not sure yet.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: TCP Server in Android
« Reply #14 on: March 11, 2020, 07:08:43 pm »

i tried to install and use for android

Error:
IdGlobal.pas(553,29) Fatal: Cannot find iconvenc used by IdGlobal

iconvenc is a FreePascal library/unit, not an Indy unit.  It should be available if your search paths are setup correctly.  Unless iconvenc is not available for Android, I don't know.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018