Internet Protocol and UNIX Domain Sockets NIO Example

This example illustrates how to intermix AF_UNIX and AF_INET/6 channels with the SocketChannel and ServerSocketChannel classes in a non-blocking client/server single-threaded application.

This example mimics some of the capabilities of the socat command-line utility. It can create listeners or clients and connect them to listeners and perform various different types of binding. Run this command with the -h option to print usage information.

Special handling is only required for the different address types at initialization. For the server side, once a listener is created and bound to an address, the code managing the selector can handle the different address families identically.

import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


import static;
import static;
import static;

public class Socat {
    static void usage() {
        String ustring = """
usage: java Socat -s <baddr>...
    java Socat -c [-bind <baddr>] <daddr> N [delay]

    java Socat -h
-s means create one or more listening servers bound to addresses <baddr>...,
then accept all incoming connections and display (counts of) received data. If
more than one <baddr> is supplied, then multiple channels are created, each
bound to one of the supplied addresses. All channels are non-blocking and
managed by one Selector.
-c means create a client, connect it to <daddr> and send N (16 Kb) buffers. The
client may optionally bind to a given address <baddr>. If a delay is specified,
then the program pauses for the specified number of milliseconds between each
send. After sending, the client reads until EOF and then exits.
Note: AF_UNIX client sockets do not bind to an address by default. Therefore,
the remote address seen on the server side (and the client's local address) is
an empty path. This is slightly different from AF_INET/6 sockets, which, if the
user does not choose a local port, then a randomly chosen one is assigned.

-h means print this message and exit.

<baddr> and <daddr> are addresses specified as follows:
{path} is the name of a socket file surrounded by curly brackets,
{}, which can be empty when binding a server signifying a randomly chosen local
{host}:port is an internet address comprising a domain name or IPv4/v6 literal
surrounded by curly brackets, {}, which can be empty when binding (signifying
any local address) and a port number, which can be zero when binding.

    static boolean isClient;
    static boolean initialized = false;
    static final int BUFSIZE = 8 * 1024;
    static int N;           // Number of buffers to send
    static int DELAY = 0;   // Milliseconds to delay between sends

    static List<AddressAndFamily> locals = new LinkedList<>();
    static AddressAndFamily remote;

    // family is only needed in cases where address is null.
    // It could be a Record type.
    static class AddressAndFamily {
        SocketAddress address;
        ProtocolFamily family;
        AddressAndFamily(ProtocolFamily family, SocketAddress address) {
            this.address = address;
   = family;

    static AddressAndFamily parseAddress(String addr) throws UnknownHostException {
        char c = addr.charAt(0);
        if (c != 'U' && c != 'I')
            throw new IllegalArgumentException("invalid address");

        String family = addr.substring(0, addr.indexOf(':')).toUpperCase();

        return switch (family) {
            case "UNIX" -> parseUnixAddress(addr);
            case "INET" -> parseInetSocketAddress(INET, addr);
            case "INET6" -> parseInetSocketAddress(INET6, addr);
            default -> throw new IllegalArgumentException();

    static AddressAndFamily parseUnixAddress(String token) {
        String path = getPathDomain(token);
        UnixDomainSocketAddress address;
        if (path.isEmpty())
              address = null;
            address = UnixDomainSocketAddress.of(path);
        return new AddressAndFamily(UNIX, address);

    static AddressAndFamily parseInetSocketAddress(StandardProtocolFamily family, String token) throws UnknownHostException {
        String domain = getPathDomain(token);
        InetAddress address;
        if (domain.isEmpty()) {
            address = (family == StandardProtocolFamily.INET)
                    ? InetAddress.getByName("")
                    : InetAddress.getByName("::0");
        } else {
            address = InetAddress.getByName(domain);
        int cp = token.lastIndexOf(':') + 1;
        int port = Integer.parseInt(token.substring(cp));
        var isa = new  InetSocketAddress(address, port);
        return new AddressAndFamily(family, isa);

    // Return the token between braces, that is, a domain name or UNIX path.
    static String getPathDomain(String s) {
        int start = s.indexOf('{') + 1;
        int end = s.indexOf('}');
        if (start == -1 || end == -1 || (start > end))
            throw new IllegalArgumentException(s);
        return s.substring(start, end);

    // Return false if the program must exit.
    static void parseArgs(String[] args) throws UnknownHostException {
        if (args[0].equals("-h")) {
        } else if (args[0].equals("-c")) {
            isClient = true;
            int nextArg;
            AddressAndFamily local = null;
            if (args[1].equals("-bind")) {
                local = parseAddress(args[2]);
                nextArg = 3;
            } else {
                nextArg = 1;
            remote = parseAddress(args[nextArg++]);
            N = Integer.parseInt(args[nextArg++]);
            if (nextArg == args.length - 1) {
                DELAY = Integer.parseInt(args[nextArg]);
            initialized = true;
        } else if (args[0].equals("-s")) {
            isClient = false;
            for (int i = 1; i < args.length; i++) {
            initialized = true;
        } else
            throw new IllegalArgumentException();

    public static void main(String[] args) throws Exception {
        try {
        } catch (Exception e) {
            System.out.printf("\nInvalid arguments supplied. See the following for usage information\n");
        if (!initialized)
        if (isClient) {
        } else {

    static Map<SocketChannel,Integer> byteCounter = new HashMap<>();

    private static void initListener(AddressAndFamily aaf, Selector selector) {
        try {
            ProtocolFamily family =;
            SocketAddress address = aaf.address;
            ServerSocketChannel server =;
            server.register(selector, SelectionKey.OP_ACCEPT, null);
            System.out.println("Server: Listening on " + server.getLocalAddress());
        } catch (IOException e) {
            throw new UncheckedIOException(e);

    private static void doServer() throws IOException {
        ByteBuffer readBuf = ByteBuffer.allocate(64 * 1024);
        final Selector selector =;
        locals.forEach(localAddress -> initListener(localAddress, selector));
        int nextConnectionId = 1;
        while (true) {
            var keys = selector.selectedKeys();
            for (SelectionKey key : keys) {
                try {
                    SelectableChannel c =;
                    if (c instanceof ServerSocketChannel) {
                        var server = (ServerSocketChannel)c;
                        var ch = server.accept();
                        var userid = "";
                        if (server.getLocalAddress() instanceof UnixDomainSocketAddress) {
                            // An illustration of additional capability of UNIX
                            // channels; it's not required behavior.
                            UnixDomainPrincipal pr = ch.getOption(ExtendedSocketOptions.SO_PEERCRED);
                            userid = "user: " + pr.user().toString() + " group: " +
                        byteCounter.put(ch, 0);
                        System.out.printf("Server: new connection\n\tfrom {%s}\n", ch.getRemoteAddress());
                        System.out.printf("\tConnection id: %s\n", nextConnectionId);
                        if (userid.length() > 0) {
                            System.out.printf("\tpeer credentials: %s\n", userid);
                        System.out.printf("\tConnection count: %d\n",  byteCounter.size());
                        ch.register(selector, SelectionKey.OP_READ, nextConnectionId++);
                    } else {
                        var ch = (SocketChannel) c;
                        int id = (Integer)key.attachment();
                        int bytes = byteCounter.get(ch);
                        int n =;
                        if (n < 0) {
                            String remote = ch.getRemoteAddress().toString();
                            System.out.printf("Server: closing connection\n\tfrom: {%s} Id: %d\n", remote, id);
                            System.out.printf("\tBytes received: %d\n", bytes);
                        } else {
                            bytes += n;
                            byteCounter.put(ch, bytes);
                            display(ch, readBuf, id);
                } catch (IOException e) {
                    throw new UncheckedIOException(e);

    private static void postBind(SocketAddress address) {
        if (address instanceof UnixDomainSocketAddress) {
            var usa = (UnixDomainSocketAddress)address;

    private static void display(SocketChannel ch, ByteBuffer readBuf, int id)
    throws IOException 
        System.out.printf("Server: received %d bytes from: {%s} Id: %d\n",
        readBuf.remaining(), ch.getRemoteAddress(), id);

    private static void doClient() throws Exception {
        SocketChannel client;
        if (locals.isEmpty())
            client =;
        else {
            AddressAndFamily aaf = locals.get(0);
            client =;
        ByteBuffer sendBuf = ByteBuffer.allocate(BUFSIZE);
        for (int i=0; i<N; i++) {
        ByteBuffer rxb = ByteBuffer.allocate(64 * 1024);
        int c;
        while ((c = > 0) {
            System.out.printf("Client: received %d bytes\n", rxb.remaining());

    private static void fill(ByteBuffer sendBuf) {
        // Because this example is for demonstration purposes, this method
        // doesn't fill the ByteBuffer sendBuf with data. Instead, it sets the
        // limits of sendBuf to its capacity and its position to zero.
        // Consequently, when the example writes the contents of sendBuf, it
        // writes the entire contents of whatever happened to be in memory when
        // sendBuf was allocated.

Example of Running the Socat Example

The following is an example of running the Socat example:

  1. In a command-line shell, run Socat as follows:

    $ java Socat -s UNIX:{/tmp/uds.sock}
    Server: Listening on /tmp/uds.sock
  2. In another command-line shell, run Socat as follows:

    $ java Socat -c UNIX:{/tmp/uds.sock} 1

    In the first command-line shell, you'll see output similar to the following:

    Server: new connection
            from {}
            Connection id: 1
            peer credentials: user: yourusername group: yourgroup
            Connection count: 1
    Server: received 8192 bytes from: {} Id: 1
    Server: closing connection
            from: {} Id: 1
            Bytes received: 8192

If you don't specify a file name when you create a UNIX domain socket, then the JVM creates a socket file and automatically binds the socket to it:

$ java Socat -s UNIX:{}
Server: Listening on /tmp/socket_837668026

This is the same as calling ServerSocketChannel.bind(null). You can change the default directory where the JVM saves automatically generated socket files by setting the system property. See Networking System Properties.