Which tech to use for wifi UDP connectivity in Android cpps

87 views
Skip to first unread message

Jon Green

unread,
Feb 9, 2021, 10:57:56 AM2/9/21
to android-ndk
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?

AppCoder

unread,
Feb 10, 2021, 6:06:44 PM2/10/21
to android-ndk
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)


Strongly suggest SWIG if you are doing C++ to java object things.

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. 

Elliott Hughes

unread,
Feb 19, 2021, 4:17:17 PM2/19/21
to android-ndk
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.
 
Reply all
Reply to author
Forward
0 new messages