junixsocket is a Java/JNI library that allows the use of Unix Domain Sockets (AF_UNIX sockets) from Java. It also provides glue code such that one can actually use RMI over AF_UNIX. junixsocket has been written by Christian Kohlschütter. It is released under the Apache 2.0 License.
Getting the files
Download the binary and/or source tarball from the Downloads page and extract the files somewhere
junixsocket consists of pure Java component that is platform-independent and a platform-dependent JNI-native C library component (for each platform a different library file is provided.)
Installing the native JNI libraries
In order to use the Java classes the JNI library needs to be installed on a particular location on your system. The following native libraries are provided in the binary tarball (directory lib-native):
You can put the native library anywhere in your java.library.path (see http://java.sun.com/docs/books/jni/html/start.html), into the path specified for the Java system property org.newsclub.net.unix.library.path and into the path stored in the LIBRARY_PATH static attribute of the NativeUnixSocketConfig class (which is /opt/newsclub/lib-native by default as a platform-independent fall-back, but this can be changed by replacing the class against a custom one).
For initial testing, it is recommended to put the libraries into /opt/newsclub/lib-native. After updating the junixsocket jars, please do not forget to also update the native libraries.
Installing the jars
junixsocket currently comes with four jar files:
The core library is required, all the others are optional.
junixsocket supports the Java Socket API. AFUNIXSocket and AFUNIXServerSocket work just as their AF_INET (TCP/IP) Socket counterparts, they extend java.net.Socketand java.net.ServerSocket respectively.
Connecting to an existing AF_UNIX Socket
junixsocket provides working demos for:
Maximum path length
At system-level the pathname of a Unix domain sockets is limited in length. The maximum varies from one OS to another. Currently, junixsocket limits the maximum pathname length to 103 bytes (this may mean less characters). If the maximum length is exceeded an Exception is thrown.
It is therefore recommended to use short pathnames whenever possible.
Unix domain server sockets are created with read-write privileges for everybody on the sytem, just like TCP/IP sockets are accessible for local users. If you want to restrict access to a particular user or group, simply create the socket in a directory that has proper access restrictions.
Whereas TCP/IP sockets point to ports at particular Internet addresses, AF_UNIX sockets point to special files on the local file system. The notion of "port" is not necessary in this case. However, we need ports for RMI. This is why AFUNIXSocketAddress also supports them. Port support may also come handy in other situations, especially when an existing application expects a particular port on a Socket. Please be aware that specifying a particular port number has no effect for non-RMI connections.
* [ Gossip in Java(2) ] 網路 : 入門 (Socket 類別)
* Java Socket Programming Examples