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?

Community
  • 1
  • 1
korrawit
  • 1,000
  • 2
  • 18
  • 30
  • 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. – Tino May 23 '15 at 19:21

4 Answers4

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.

Aleksander Blomskøld
  • 18,374
  • 9
  • 76
  • 82
Christian Kohlschütter
  • 3,032
  • 1
  • 18
  • 13
  • 2
    As at Nov 2015, the *junixsocket* project resides at https://github.com/kohlschutter/junixsocket – VirtualMichael Nov 04 '15 at 05:55
  • 6
    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](http://stackoverflow.com/questions/13953912). – Florian Mar 16 '17 at 15:43
  • 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. – Per Lundberg Sep 09 '22 at 11:07
  • junixsocket now supports both stream and datagram sockets. – Christian Kohlschütter Oct 14 '22 at 12:30
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.

nogudnik
  • 247
  • 2
  • 3
  • 7
    As of Java 16, Unix domain sockets are supported natively java with SocketChannel / ServerSocketChannel API. – Benny May 26 '21 at 17:20
8

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

Leo Gomes
  • 1,113
  • 9
  • 14
  • 4
    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](http://stackoverflow.com/questions/13953912). – Florian Mar 16 '17 at 15:44
  • 1
    Please note Netty 4.0 only support `epoll(2)` for Unix domain socket, this means it can't be used without Linux. – Low power Mar 22 '20 at 13:34
4

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);

Paul Rubel
  • 26,632
  • 7
  • 60
  • 80