Introduction
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
Installation
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.
Basic Usage
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
- File socketFile = new File("/path/to/your/socket");
- AFUNIXSocket sock = AFUNIXSocket.newInstance();
- sock.connect(new AFUNIXSocketAddress(socketFile));
- File socketFile = new File("/path/to/your/socket");
- AFUNIXServerSocket server = AFUNIXServerSocket.newInstance();
- server.bind(new AFUNIXSocketAddress(socketFile));
- import org.newsclub.net.unix.AFUNIXServerSocket
- import org.newsclub.net.unix.AFUNIXSocket
- import org.newsclub.net.unix.AFUNIXSocketAddress
- pathToSocket="/var/tmp/test_socket"
- File socketFile = new File(pathToSocket)
- boolean socketReady=false
- // Server Socket
- Thread.start {
- printf("\t[Info] Open Server Socket...\n")
- AFUNIXServerSocket server = AFUNIXServerSocket.newInstance()
- server.bind(new AFUNIXSocketAddress(socketFile))
- socketReady=true
- Socket socket = server.accept()
- try
- {
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))
- bw.writeLine("Hello World!")
- bw.close()
- }
- catch(Exception e)
- {
- e.printStackTrace()
- }
- finally
- {
- socket.close()
- }
- sleep 1000
- printf("\t[Info] Close Server Socket...\n")
- server.close()
- }
- // Client Socket
- while(!socketReady) sleep(1000)
- printf("\t[Info] Open Client Socket...\n")
- AFUNIXSocket sock = AFUNIXSocket.newInstance();
- sock.connect(new AFUNIXSocketAddress(socketFile));
- BufferedReader input =new BufferedReader(new InputStreamReader(sock.getInputStream()))
- String line=null
- printf("\t[Info] Read from Socket:\n")
- while((line=input.readLine())!=null)
- {
- printf("\t\t%s\n", line)
- }
- printf("\t[Info] Close Client Socket...\n")
- sock.close()
junixsocket provides working demos for:
Caveats
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.
Security
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.
Ports
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.
Supplement
* [ Gossip in Java(2) ] 網路 : 入門 (Socket 類別)
* Java Socket Programming Examples
沒有留言:
張貼留言