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 Lazarus' Online Package Manager (https://wiki.freepascal.org/Online_Package_Manager).
Title: Re: TCP Server in Android
Post by: zeljkoc on March 07, 2020, 07:31:37 pm
Please send link indy 10.6.2 for lazarus
Title: Re: TCP Server in Android
Post by: Remy Lebeau 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 (https://wiki.freepascal.org/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 (https://www.indyproject.org/2019/11/28/indy-svn-retiring-long-live-github/) a few month ago, but it should be fairly recent.
Title: Re: TCP Server in Android
Post by: GetMem 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. 
Title: Re: TCP Server in Android
Post by: zeljkoc 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
Title: Re: TCP Server in Android
Post by: Remy Lebeau 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 (https://help.github.com/en/github/importing-your-projects-to-github/support-for-subversion-clients), 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 (https://semver.org/) instead.  Not sure yet.
Title: Re: TCP Server in Android
Post by: Remy Lebeau 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 (https://wiki.lazarus.freepascal.org/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.
Title: Re: TCP Server in Android
Post by: zeljkoc on March 12, 2020, 12:48:36 pm
Yes, the problem is with android.
Others have the same problem

https://www.pilotlogic.com/sitejoom/index.php/forum/cross-build-development/3820-compile-android-library-include-indy-components-failed-cannot-find-liconv
Title: Re: TCP Server in Android
Post by: trev on March 12, 2020, 01:51:42 pm
See https://stackoverflow.com/questions/10004077/how-to-install-libiconv-for-android-ndk

Does Indy depend on iconv?
Title: Re: TCP Server in Android
Post by: DonAlfredo on March 12, 2020, 03:11:34 pm
I have build libiconv for Android.
You might try if this works for you.

https://github.com/LongDirtyAnimAlf/fpcupdeluxe/releases/download/1.6.8c/libiconv_android.zip
Title: Re: TCP Server in Android
Post by: Remy Lebeau on March 12, 2020, 08:05:35 pm
Yes, the problem is with android.
Others have the same problem

https://www.pilotlogic.com/sitejoom/index.php/forum/cross-build-development/3820-compile-android-library-include-indy-components-failed-cannot-find-liconv

That is not the same error that zeljkoc is experiencing.  That error shows the iconvenc unit being compiled fine (not including some hints), the project is just not linking to the actual iconv library.

In zeljkoc's case, the compiler is not even finding the iconvenc unit at all.

Does Indy depend on iconv?

On 'Nix platforms, yes.  Android uses Java running on top of Linux, and being that Indy is a native code library, it runs at the Linux layer, not at the Java layer.

Indy does a lot of operations that have to encode Unicode strings to byte arrays, and decode byte arrays to Unicode strings.  Now, certainly Indy could be updated to handle UTF encodings manually at least, but seeing that there are still many charsets being used in the world other than UTFs, especially for things like email, it would not be feasible for Indy to handle all of those charsets manually, so it uses platform APIs to handle them, such as iconv on 'Nix.

There are some higher level hooks available in Indy if you want to handle charsets manually in your own code (like the GIdEncodingNeeded event in the IdGlobalProtocols unit, and various methods that take a user-supplied IIdTextEncoding as a parameter), but I don't think there are enough hooks available at the lower level to completely break the iconv dependency at this time.  For instance, you could disable the USE_ICONV define in IdCompilerDefines.inc, but then you will have to manually provide an implementation for the TIdMBCSEncoding class in the IdGlobal unit, or else Indy's handling of UTF-7, UTF-8, and other non-UTF charsets will break at runtime.
Title: Re: TCP Server in Android
Post by: eljo on March 12, 2020, 08:29:49 pm
Yes, the problem is with android.
Others have the same problem

https://www.pilotlogic.com/sitejoom/index.php/forum/cross-build-development/3820-compile-android-library-include-indy-components-failed-cannot-find-liconv

That is not the same error that zeljkoc is experiencing.  That error shows the iconvenc unit being compiled fine (not including some hints), the project is just not linking to the actual iconv library.

In zeljkoc's case, the compiler is not even finding the iconvenc unit at all.

Does Indy depend on iconv?

On 'Nix platforms, yes.  Android uses Java running on top of Linux, and being that Indy is a native code library, it runs at the Linux layer, not at the Java layer.

have you seen this https://github.com/d-mozulyov/UniConv ? it might be a better option don't know.

since you are here I'd like to ask if there is any documentation on what is required to port indy to a new OS I'm contemplating of porting to ultibo and wandering how complicated that might be.
Title: Re: TCP Server in Android
Post by: GetMem on March 14, 2020, 10:21:08 am
@Remy Lebeau
Quote
Actually, GitHub does have an SVN interface, which does have a revision number.
Thanks. I will use the revision number from the SVN interface.
Title: Re: TCP Server in Android
Post by: Remy Lebeau on March 14, 2020, 11:27:23 pm
have you seen this https://github.com/d-mozulyov/UniConv ? it might be a better option don't know.

Interesting, but the goal is to not have to embed huge conversion tables into every app that wants to use Indy.  That is why Indy delegates to platform APIs for text conversions, let the OS handle them.

since you are here I'd like to ask if there is any documentation on what is required to port indy to a new OS

No, there is not.

I'm contemplating of porting to ultibo and wandering how complicated that might be.

Never heard of ultibo.  I don't have a clue what the requirements are for writing code for it.
Title: Re: TCP Server in Android
Post by: asdf1337 on March 14, 2020, 11:50:02 pm
I'm contemplating of porting to ultibo and wandering how complicated that might be.

Never heard of ultibo.  I don't have a clue what the requirements are for writing code for it.

Would start with seeing what happens if you just compile it - maybe it works without that many changes if used the right defines (e.g. ultibo has Winsock 2.0 interface).
Otherwise you need to do all the low-level calls they use for creating/sending/receiving data through the socket (see https://ultibo.org/wiki/Unit_Reference#Network_units).
Title: Re: TCP Server in Android
Post by: eljo on March 15, 2020, 12:37:35 am
have you seen this https://github.com/d-mozulyov/UniConv ? it might be a better option don't know.

Interesting, but the goal is to not have to embed huge conversion tables into every app that wants to use Indy.  That is why Indy delegates to platform APIs for text conversions, let the OS handle them.

since you are here I'd like to ask if there is any documentation on what is required to port indy to a new OS

No, there is not.
A generic group list should get me started I guess.
so

1) sockets api (already provided)
2) uniconv  (as showed in this thread).

threading? something else that I might hit my head on?

Any way thank you for the info.


I'm contemplating of porting to ultibo and wandering how complicated that might be.

Never heard of ultibo.  I don't have a clue what the requirements are for writing code for it.
https://ultibo.org a baremetal framework to write your own firmware for raspberry PI. It has
 units for a lot of things.
Title: Re: TCP Server in Android
Post by: Remy Lebeau on March 15, 2020, 09:58:33 pm
A generic group list should get me started I guess.

It hass been a long time since Indy added a new OS.  All I can really offer you is this for now:

- go through IdCompilerDefines.inc and see if anything needs to be added/adjusted for ultibo.

- go through IdGlobal.pas and see if anything needs to be added/adjusted for ultibo.  Type declarations, handling of text encodings, etc.

- check if the existing TIdStack-derived classes are suitable for ultibo's socket API.  If not, you will have to write a new class, and then update IdStack.pas to assign that class type to the global GStackClass variable in the unit's initialization section.

threading?

Indy uses the RTL's TThread class, so threading support is the responsibility of FreePascal to handle.
TinyPortal © 2005-2018