18

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
  • 960
  • 2
  • 17
  • 29
  • 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,064
  • 9
  • 74
  • 82
Christian Kohlschütter
  • 2,954
  • 1
  • 17
  • 12
  • 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
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
  • 4
    As of Java 16, Unix domain sockets are supported natively java with SocketChannel / ServerSocketChannel API. – Benny May 26 '21 at 17:20
7

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

Leo Gomes
  • 1,073
  • 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
3

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
  • 25,810
  • 7
  • 57
  • 77