Android: Unix Domain Socket

Budhdi Sharma
Android Champ
Published in
5 min readFeb 8, 2023

--

android

IPC (Inter-Process Communication) is a mechanism that allows multiple processes to communicate with each other on an Android device.

Unix Domain Sockets, also known as Inter-Process Communication (IPC) sockets, are a form of IPC that allow communication between processes on the same device. In the world of Android, Unix Domain Sockets provide a secure and efficient way for apps to communicate with each other and with the operating system.

The Linux operating system provides an inter-process communication method of the UNIX domain protocol, which cannot be applied in the network, but can be used in the communication between two processes of the machine. It can conveniently pass file descriptors between two non-related processes, and the effect is similar to passing between parent and child processes. UNIX domain sockets are more efficient when interacting with local processes, because it does not need to handle the possibility of network exceptions. So one of the key advantages of Unix Domain Sockets is that they allow for low-level, fast communication between processes without incurring the overhead of network communication.

So it enables to have bidirectional communication between processes in the same machine. It supports both stream-oriented (TCP) and datagram-oriented (UDP) protocols.

Differences between Unix Domain Socket & Network socket

Unix domain sockets provide a communication channel between processes on the same host. They are an efficient and secure means of communication because they operate within the operating system’s kernel, bypassing the overhead of network communication. Unix domain sockets use file-system paths as addresses, which allows them to take advantage of the permissions and security features of the file system.

Network sockets, on the other hand, provide communication between processes running on different hosts connected by a network. Network sockets use network addresses such as IP addresses and port numbers to identify the endpoints of a communication channel. Network sockets are less secure than Unix domain sockets, as the communication is done over a network, which is more susceptible to eavesdropping and tampering.

How to Implement it in Android

In Android, you can implement Unix domain sockets using the LocalSocket and LocalServerSocket classes in the android.net.LocalSocket and android.net.LocalServerSocket packages.

Here is a basic example of how you can use these classes to implement a simple IPC scenario:

  1. Creating a server socket:
//Java
LocalServerSocket server = new LocalServerSocket("socket_name");
LocalSocket socket = server.accept();


//C
int server_socket = socket(AF_UNIX, SOCK_STREAM, 0);
struct sockaddr_un server_address;
memset(&server_address, 0, sizeof(server_address));
server_address.sun_family = AF_UNIX;
strcpy(server_address.sun_path, "socket_name");
bind(server_socket, (struct sockaddr *) &server_address, sizeof(server_address));
listen(server_socket, 5);
int client_socket = accept(server_socket, NULL, NULL);

2. Creating a client socket:

//Java
LocalSocket client = new LocalSocket();
client.connect(new LocalSocketAddress("socket_name"));


//C
int client_socket = socket(AF_UNIX, SOCK_STREAM, 0);
struct sockaddr_un server_address;
memset(&server_address, 0, sizeof(server_address));
server_address.sun_family = AF_UNIX;
strcpy(server_address.sun_path, "socket_name");
connect(client_socket, (struct sockaddr *) &server_address, sizeof(server_address));

3. Sending data:

//Java
OutputStream os = client.getOutputStream();
os.write("Hello from the client".getBytes());

//C
const char *message = "Hello from the client";
send(client_socket, message, strlen(message), 0);

4. Receiving data:

//Java
InputStream is = socket.getInputStream();
byte[] buffer = new byte[1024];
is.read(buffer);
String message = new String(buffer);

//C
char buffer[1024];
recv(client_socket, buffer, 1024, 0);
std::string message(buffer);

So basically the overall concept at both server and client side is:

Server Side

  1. Create a new socket
  2. Accept connection
  3. Get input/output stream
  4. Read/Write to the stream

Client Side

  1. Open a created socket with the same name as the server
  2. Connect to the socket
  3. get input/output stream
  4. Read/Write to the stream

Conclusion

Unix domain sockets have some disadvantages, which are as follows:

  1. Limited scope of use: Unix domain sockets are limited to communication between processes on the same host, which means that they cannot be used for communication between processes running on different hosts.
  2. Higher overhead for larger data transfers: Unix domain sockets have lower overhead compared to network sockets for small amounts of data, but for larger data transfers, the overhead can be higher due to the added security and permissions checks performed by the file system.
  3. Security concerns: Unix domain sockets are based on the file system and therefore subject to the same security and permissions issues as regular files. For example, if a malicious process gains access to the file system, it could potentially access or modify the data transmitted through a Unix domain socket.
  4. Lack of support for network-related protocols: Unix domain sockets do not support network-related protocols such as TCP or UDP, which means that you may need to implement additional protocols to support certain functionality that is typically provided by these protocols.
  5. Complexity: Unix domain sockets can be more complex to use than network sockets, as they are based on the file system and require a deeper understanding of the underlying mechanism.

Unix domain sockets have some advantages, which are as follows:

  1. Low overhead: Unix domain sockets have lower overhead compared to network sockets because they do not require the overhead of network-related protocols such as TCP or UDP. This makes Unix domain sockets particularly suitable for high-performance, low-latency communication between processes on the same host.
  2. Simplified communication: Unix domain sockets provide a simple and convenient way to communicate between processes on the same host, as they do not require IP addresses or port numbers to be specified.
  3. Improved security: Unix domain sockets provide better security compared to network sockets because they use the file system’s security and permissions model. This means that access to a Unix domain socket can be restricted to specific processes, providing a higher level of protection against malicious access.
  4. Inter process communication: Unix domain sockets allow inter-process communication (IPC) between processes on the same host. This makes it easier to write and deploy complex, multi-process systems, as communication between the processes can be done using standard file I/O operations.
  5. Efficient communication: Unix domain sockets are efficient for communication between processes on the same host because they use a memory-mapped file for communication, which eliminates the need for copying data between user and kernel space.

In conclusion, Unix domain sockets are a powerful tool for communication between processes on the same host, as they provide low overhead, simplified communication, improved security, efficient inter-process communication, and improved performance compared to network sockets.

--

--

Budhdi Sharma
Android Champ

As an AOSP developer, I specialize in creating robust framework and system applications that seamlessly integrate with embedded systems on various SOCs