using unix domain sockets

2,083 views
Skip to first unread message

herc

unread,
Mar 28, 2012, 5:55:23 AM3/28/12
to android-ndk
Hello All,


I want to talk between Java and native code using local domain
sockets. Can someone tell me how I have to do that ?
I create a LocalServerSocket in java but If I try to connect to this
local socket from my C code, I get an error.


Regards

freakingtux

unread,
Mar 29, 2012, 7:54:47 AM3/29/12
to andro...@googlegroups.com
Hello,
There are a few example of this kind of interaction between different Android components. mainly between the system server and the other daemons rild, vold and installd.

Best would be to also post the error you get but most probably your are lacking permissions to create sockets or are running as different users) 
Greetings

Nasif Noorudeen

unread,
Mar 29, 2012, 12:06:09 PM3/29/12
to andro...@googlegroups.com
LocalSocket(AF_UNIX) sockets are file sockets, of course there is possible to communicate between this two(either if one is in c and one is in java) proviede that both both socket must be in same process.

Please post your error here

Thanks

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/android-ndk/-/O3LiHsnmtpQJ.

To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.

Chris Stratton

unread,
Mar 29, 2012, 2:43:58 PM3/29/12
to andro...@googlegroups.com
On Thursday, March 29, 2012 12:06:09 PM UTC-4, nasif wrote:
LocalSocket(AF_UNIX) sockets are file sockets, of course there is possible to communicate between this two(either if one is in c and one is in java) proviede that both both socket must be in same process.

There is no requirement that both sockets be in the same process.  In the most common use cases, they they are not. 

David Given

unread,
Mar 30, 2012, 9:30:12 AM3/30/12
to andro...@googlegroups.com
Chris Stratton wrote:
[...]

> There is no requirement that both sockets be in the same process. In
> the most common use cases, they they are not.

We're using Unix sockets to communicate between two processes (one side
is a Dalvik VM, the other is a daemon process started with fork), and it
all works fine.

However, bear in mind that a Java LocalSocketAddress actually refers to
an abstract socket address, not a filesystem one --- from the C side,
the first byte of the address must be a \0. See the man page.

--
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│ "Parents let children ride bicycles on the street. But parents do not
│ allow children to hear vulgar words. Therefore we can deduce that
│ cursing is more dangerous than being hit by a car." --- Scott Adams

signature.asc

David Given

unread,
Jan 14, 2013, 9:28:13 AM1/14/13
to andro...@googlegroups.com
Ajit Kumar wrote:
> David, do you have an example for the java code for dalvik vm. Do you
> use java.net.Socket/ServerSocket or
> android.net.LocalSocket/LocalServerSocket?

Sorry, I should have been clearer: both sides of our code are native, so
we're not using Java. I have used androdi.net.LocalSocket in the past
but don't have any code handy, sorry.

(You have to use android.net.LocalSocket as java.net.Socket doesn't
support Unix domain sockets. However... there's nothing to stop you
connecting to localhost if you prefer to use Socket: it's marginally
less efficient than a Unix domain socket, but only very slightly.)

--
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│ "There is nothing in the world so dangerous --- and I mean *nothing*
│ --- as a children's story that happens to be true." --- Master Li Kao,
│ _The Bridge of Birds_

signature.asc
Reply all
Reply to author
Forward
0 new messages