19

I see these threads UNIX socket implementation for Java? and http://forums.sun.com/thread.jspa?threadID=713266.

The second link says that Java already supports UNIX Domain Socket. If that's true what class do I need to implement from Java?.

From the first link, it says that Java does not support UNIX Domain Socket. If you need UNIX Domain Socket you must use a 3rd-party library.

So, which is it?

1
  • 4
    The second link (forums.sun.com) is broken and seems to lead to some nowhere leading page of Oracle. No archive.org found, BTW, sadly. Commented May 23, 2015 at 19:21

4 Answers 4

24

You could use junixsocket: https://github.com/kohlschutter/junixsocket

It provides AF-UNIX support via a JNI library, utilizing the Java Socket API. It even allows connecting to MySQL from Java (Connector/J) via Unix sockets.

Sign up to request clarification or add additional context in comments.

4 Comments

As at Nov 2015, the junixsocket project resides at github.com/kohlschutter/junixsocket
Be careful, it only supports Unix sockets in STREAM mode. Check what your server opens, with netstat -ux. See also Difference between UNIX domain STREAM and DATAGRAM sockets.
Followup to @Florian's great comment: On modern systems, netstat is often unavailable, whereas the iproute2 package and its ss tool has replaced it. Use ss -lx to see processes listening to UNIX domain sockets in this case.
junixsocket now supports both stream and datagram sockets.
17

Java cannot create or access Unix Domain Sockets without using a 3rd party (native) library. The last comment on the second link above mentions this.

The first link has some good (and correct) information on it.

1 Comment

As of Java 16, Unix domain sockets are supported natively java with SocketChannel / ServerSocketChannel API.
8

Netty also supports it since version 4.0.26: https://github.com/netty/netty/pull/3344

2 Comments

Be careful, it only supports Unix sockets in STREAM mode. Check what your server opens, with netstat -ux. See also Difference between UNIX domain STREAM and DATAGRAM sockets.
Please note Netty 4.0 only support epoll(2) for Unix domain socket, this means it can't be used without Linux.
5

As noted by @Benny in a comment, JDK 16 comes with built-in support for unix domain sockets via java.net.UnixDomainSocketAdress and related classes. You can read more at JEP-380

Here's a snippit from the JEP:

var unixAddr = UnixDomainSocketAddress.of("/foo/bar.socket");
var channel2 = SocketChannel.open(unixAddr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.