1

I got a native app that opens a UnixDomain socket with this code.

struct sockaddr_un local;
int len;
int fd;

fd = socket(AF_UNIX, SOCK_DGRAM, 0);

local.sun_family = AF_UNIX; 
strcpy(local.sun_path, "path.to.socket");

len = strlen(local.sun_path) + sizeof(local.sun_family);

bind(fd, (struct sockaddr*)&local, sizeof(local));

The code above is working because I can see that the socket is created.

Now I want to send a UDP packet from my android app written in Java. I believe I need to use the LocalSocket class. The problem is I don't know how to use the LocalSocket class for UDP. All the tutorials I see is for TCP(SOCK_STREAM).

I tried connecting to the created socket using the codes below but they are giving me errors

LocalSocket socket = new LocalSocket(); 
socket.connect(new LocalSocketAddress( "path.to.socket" ));

This gives me Connection refused error

I also tried binding to the same file but it shows the Address already in use error.

Can I use the LocalSocket class for UDP or it was designed for TCP only?

kuchi
  • 840
  • 11
  • 19
  • Why are you using `UDP` anyways, they are not reliable. At some point of time, you'll run into issues of buffers of pending packets filling up, and consequently packets will be dropped. How the network subsystem drops packets is implementation-dependent and is not specified anywhere. – The Dark Knight Jun 11 '13 at 08:19
  • What is the `socket address` that you are using ? Is it something like '127.0.0.1' ? On your Android emulation (and Android device), an address like 127.0.0.1 means the Android emulation machine, not the host PC. You can access your host at 10.0.2.2. – The Dark Knight Jun 11 '13 at 08:21
  • I am not using 127.0.0.1. I am using Unix Domain sockets, instead of IP addresses I am using files, you can compare it to named pipes in Windows. – kuchi Jun 11 '13 at 08:48

2 Answers2

4

Problem

I see several issues with your code:

  1. In your java code, the LocalSocketAddress defaults to the ABSTRACT namespace. However, your native app opens a socket in the LocalSocketAddress.Namespace.FILESYSTEM namespace. However, getting the "address already in use" error implies that the addressing works; this may indicate that your addresses match despite the incorrect namespace.

  2. The java code does not pass the SOCKET_DGRAM type to the constructor; this is new as of API 19.

Fixes

Address space

  • either specify the FILESYSTEM namespace when creating the LocalSocketAddress, or
  • in the native app, create the socket in the abstract namespace by prepending a '\0' NUL byte. See also: bind(2).

Socket Type

LocalSocket socket = new LocalSocket(SOCKET_DGRAM);
user3779342
  • 156
  • 5
  • but how send data as LocalSocket only provide outputStream but my socket is UDP, means no connection. – Jiang YD Dec 14 '18 at 04:30
-1

If you want to send UDP packets, you should use java.net.DatagramSocket. Here you can find a small but good tutorial.

Jonathan Kortleven
  • 613
  • 1
  • 5
  • 16
  • I need to use LocalSocket and not DatagramSocket since it is for Unix Domain sockets. My app was originaly using DatagramSocket but I now need to send file descriptors to my native app that is why I am trying to convert it to LocalSocket. – kuchi Jun 11 '13 at 08:51
  • So, if I understand correctly, you're trying to communicate between two apps on the same Android system using UDP packets? – Jonathan Kortleven Jun 11 '13 at 09:17
  • Yes, Here's (http://osr507doc.sco.com/en/netguide/dusockT.datagram_code_samples.html) a sample code that uses datagrams via UNIX Domains. The server and client are both written in C, what I want to do is convert the client code in Java version. – kuchi Jun 11 '13 at 11:14