Which tech to use for wifi UDP connectivity in Android cpps

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Which tech to use for wifi UDP connectivity in Android cpps

Jon Green
I am writing an NDK app and want to add UDP connectivity over wifi. I could use java.net to create sockets, but are they efficient? Most of my code is C++ so I need to use JNI to communicate with the java socket layer. How can I efficiently pass buffers of packet data of arbitrary length (sequences of bytes) via JNI between the java layer and the C++ layer?

Is there any reason I should not use linux sockets, which avoid the need for the java/JNI interface? It would seem much easier, but is it discouraged, or regarded as bad practice? And would there be technical problems? Are they less secure?

I can't find any NDK wrapper for UDP sockets - I presume there is no such module?

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/fd792f4b-c2c1-4bee-9153-1fcd6a838ea4n%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Which tech to use for wifi UDP connectivity in Android cpps

Dan-7
On Tuesday, February 9, 2021 at 9:57:56 AM UTC-6 Jon Green wrote:
I am writing an NDK app and want to add UDP connectivity over wifi. I could use java.net to create sockets, but are they efficient? Most of my code is C++ so I need to use JNI to communicate with the java socket layer. How can I efficiently pass buffers of packet data of arbitrary length (sequences of bytes) via JNI between the java layer and the C++ layer?

java.nio.ByteBuffer is the best choice for getting a block of bytes to C/C++.
Sadly, you don't want to be making a new one and letting the old one get
grabbed by GC because a GC sweep is expensive (allocate and reuse one
with a max size if you know it and are doing serial operations)

https://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html

Strongly suggest SWIG if you are doing C++ to java object things.
http://www.swig.org/Doc1.3/Java.html

I believe the android java api of choice is the DatagramSocket class
if you want to run pure java.
 
Is there any reason I should not use linux sockets, which avoid the need for the java/JNI interface? It would seem much easier, but is it discouraged, or regarded as bad practice? And would there be technical problems? Are they less secure?

If you are already using native code, no reason to do the JNI bridge.
 
I can't find any NDK wrapper for UDP sockets - I presume there is no such module?

I suspect not. 

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/f802bbf7-e493-48bb-ac26-68e2e3d47433n%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Which tech to use for wifi UDP connectivity in Android cpps

Elliott Hughes
In reply to this post by Jon Green


On Tuesday, February 9, 2021 at 7:57:56 AM UTC-8 Jon Green wrote:
I am writing an NDK app and want to add UDP connectivity over wifi. I could use java.net to create sockets, but are they efficient? Most of my code is C++ so I need to use JNI to communicate with the java socket layer. How can I efficiently pass buffers of packet data of arbitrary length (sequences of bytes) via JNI between the java layer and the C++ layer?

Is there any reason I should not use linux sockets, which avoid the need for the java/JNI interface? It would seem much easier, but is it discouraged, or regarded as bad practice? And would there be technical problems? Are they less secure?

if (as you said above) you have mostly C++, it almost certainly makes more sense for you to just use the POSIX socket API. it will be much less complicated than going back and forth between Java and C++, and faster. it will also be more portable.
 
I can't find any NDK wrapper for UDP sockets - I presume there is no such module?

no, because you'd just use https://man7.org/linux/man-pages/man2/socket.2.html and friends instead.
 

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/b78120c6-624b-4433-ae25-a278cfafe914n%40googlegroups.com.