7

Is there a way to use Unix Domain sockets with Java NIO? I want to use NIO so that I can use Selectors on it in a single thread.

I had a look at junixsocket but it only seems to support normal Sockets not NIO channels that support selectors.

jbx
  • 21,365
  • 18
  • 90
  • 144

4 Answers4

9

You can use the project jnr-unixsocket,(https://github.com/jnr/jnr-unixsocket) which is the far most advanced implementation I've seen. It does exactely what you want : nio concepts Selectors, Channels etc

Look @ https://github.com/jnr/jnr-unixsocket/tree/master/src/test/java/jnr/unixsocket/example

Elist
  • 5,313
  • 3
  • 35
  • 73
taharqa
  • 1,112
  • 1
  • 13
  • 19
  • you're welcome. I manage to read and write http request from nginx proxy to unix socket domain with it. – taharqa Jul 04 '13 at 22:08
2

JDK 16 has built-in support for Unix Domain Sockets. Here's an example from Oracle about using it with NIO

Paul Rubel
  • 26,632
  • 7
  • 60
  • 80
  • That's great to hear. The project I was working on has long finished, but it's good to know for any future projects. – jbx Oct 02 '21 at 20:11
1

I confirm XNIO supports the use of Unix domain sockets, as "local sockets". In addition to release note mention, here are:

But none of XNIO channels implement SelectableChannel so I fear you will not be able to use Selectors.

I propose you post an issue to improve this situation, both in XNIO and also in GNU ClassPath project where LocalSocket.getSocketChannel returns null.

Yves Martin
  • 10,217
  • 2
  • 38
  • 77
  • Thanks. Its a pity that XNIO does not implement Selectors, its one of the major advantages of NIO. – jbx Mar 13 '12 at 09:06
  • Although the answer does not include support for Selectors its the best answer. – jbx Mar 14 '12 at 11:09
  • 1
    Update: Got confirmation from the actual developer on XNIO that in fact it does not support Unix Domain Socket. The interface defines it but the implementation does not yet exist. An implementation is in the making ... – jbx Apr 09 '12 at 20:44
  • So hold on.... Your answer says you confirm XNIO supports unix sockets, but there is a comment here that says it doesn't. Only one of you is correct. I am downvoting this answer until this gets resolved. – Mike Pone Feb 19 '14 at 17:37
  • Wow, this is a pretty old thread. At that point in time the XNIO API had an interface that allowed you to choose unix sockets, however I had got a reply from one of the developers that an implementation was still not available. Maybe this has changed, its nearly 2 years now since I asked. – jbx Feb 21 '14 at 12:23
  • I looked in the latest 3.2 beta code and it looks like the API is still there with no implementation. http://grepcode.com/file/repo1.maven.org/maven2/org.jboss.xnio/xnio-api/3.2.0.Beta3/org/xnio/XnioWorker.java?av=f – Mike Pone Mar 24 '14 at 15:41
0

junixsocket now has support for NIO, Selectors and all.

Please give it another try: https://github.com/kohlschutter/junixsocket

Christian Kohlschütter
  • 3,032
  • 1
  • 18
  • 13