Backend Development 6 min read

Implementing Unix Domain Socket Communication in Java with junixsocket

This article demonstrates how to use the junixsocket library to add Unix domain socket support to Java applications, providing Maven dependency details, full server and client code examples, and cross‑language testing with a Go server implementation.

FunTester
FunTester
FunTester
Implementing Unix Domain Socket Communication in Java with junixsocket

The article begins by noting the author's previous work on Unix socket support in Go and introduces the need for a similar implementation in Java, which requires adding an external dependency.

<dependency>
    <groupId>com.kohlschutter.junixsocket</groupId>
    <artifactId>junixsocket-core</artifactId>
    <version>2.3.3</version>
</dependency>

Java Implementation

Compared with Go, the Java code is more verbose, but the author is comfortable with Java.

Server

The server mirrors the functionality described earlier: it starts, listens for connections, reads messages, and replies.

package com.funtest.unix_socket;

import org.newsclub.net.unix.AFUNIXServerSocket;
import org.newsclub.net.unix.AFUNIXSocketAddress;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Unix Socket server
 */
public class UnixSocketServer {
    public static void main(String[] args) throws IOException {
        File socketFile = new File("/Users/oker/logs/temp/unix_socket_example"); // server listening file
        try (ServerSocket server = AFUNIXServerSocket.newInstance()) { // create server
            server.bind(new AFUNIXSocketAddress(socketFile)); // bind file
            System.out.println("Listening file: " + socketFile);
            while (true) {
                try (Socket socket = server.accept()) { // accept client
                    try (BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                         PrintWriter writer = new PrintWriter(socket.getOutputStream(), true)) { // reply to client
                        String line;
                        while ((line = reader.readLine()) != null) { // read client messages
                            System.out.println("Received: " + line);
                            writer.println("Reply: " + line); // send reply
                        }
                    }
                }
            }
        }
    }
}

Client

The client simply sends a message and prints the response.

package com.funtest.unix_socket;

import org.newsclub.net.unix.AFUNIXSocket;
import org.newsclub.net.unix.AFUNIXSocketAddress;

import java.io.*;
import java.net.Socket;

/**
 * Unix Socket client
 */
public class UnixScoketClient {
    public static void main(String[] args) throws IOException {
        File socketFile = new File("/Users/oker/logs/temp/unix_socket_example"); // server listening file
        try (Socket socket = AFUNIXSocket.newInstance()) { // create client
            socket.connect(new AFUNIXSocketAddress(socketFile)); // connect to server
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                 PrintWriter writer = new PrintWriter(socket.getOutputStream(), true)) { // send message
                writer.println("Hello FunTester");
                String response = reader.readLine(); // read server response
                System.out.println("Received response: " + response);
            }
        }
    }
}

Testing Interoperability

Running the Java server and client shows the server printing received messages and the client printing the echoed response. When the same client talks to a Go server, the Go server initially does not send a response, resulting in a null reply on the Java side.

Listening file: /Users/oker/logs/temp/unix_socket_example
Received message: Hello FunTester
Received message: Hello FunTester
Received message: Hello FunTester
Received response: Reply: Hello FunTester

Process exited with code 0

To make the Go server return a response, the author adds a simple handler that reads the incoming data and writes back a greeting.

//handleConnection processes a connection
func handleConnection(conn net.Conn) {
    defer conn.Close()           // close connection
    buffer := make([]byte, 1024) // create buffer
    n, err := conn.Read(buffer)   // read data
    if err != nil {              // read error
        fmt.Println("Read error:", err)
        return
    }
    conn.Write([]byte("Hello FunTester"))        // write data
    fmt.Printf("Received message: %s\n", string(buffer[:n])) // print received data
}
backendJavaIPCNetwork Programmingjunixsocketunix socket
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.