2014年11月6日 星期四

[ Java 套件 ] junixsocket - Unix Domain Socket 4J

Source From Here 
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. 
A Unix domain socket or IPC socket (inter-process communication socket) is a data communications endpoint for exchanging data between processes executing within the same host operating system. While similar in functionality to named pipes, Unix domain sockets may be created as connection‑mode (SOCK_STREAM orSOCK_SEQPACKET) or as connectionless (SOCK_DGRAM), while pipes are streams only.

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): 
* libjunixsocket-linux-1.5-amd64.so Linux Intel 64bit
* libjunixsocket-linux-1.5-i386.so Linux Intel 32bit
* libjunixsocket-macosx-1.5-i386.dylib OS X Intel 32bit
* libjunixsocket-macosx-1.5-x86_64.dylib OS X Intel 64bit

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: 
* junixsocket-X.Y.jar The core library
* junixsocket-rmi-X.Y.jar The RMI extension (RMI over AF_UNIX)
* junixsocket-mysql-X.Y.jar MySQL support
* junixsocket-demo-X.Y.jar Demo classes (the jar archive contains the Java sources, too).

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 
  1. File socketFile = new File("/path/to/your/socket");  
  2. AFUNIXSocket sock = AFUNIXSocket.newInstance();  
  3. sock.connect(new AFUNIXSocketAddress(socketFile));  
Creating a new AF_UNIX Server Socket 
  1. File socketFile = new File("/path/to/your/socket");  
  2. AFUNIXServerSocket server = AFUNIXServerSocket.newInstance();  
  3. server.bind(new AFUNIXSocketAddress(socketFile));  
Groovy code of demonstration 
  1. import org.newsclub.net.unix.AFUNIXServerSocket  
  2. import org.newsclub.net.unix.AFUNIXSocket  
  3. import org.newsclub.net.unix.AFUNIXSocketAddress  
  4.   
  5. pathToSocket="/var/tmp/test_socket"  
  6. File socketFile = new File(pathToSocket)  
  7. boolean socketReady=false  
  8.   
  9. // Server Socket  
  10. Thread.start {  
  11.     printf("\t[Info] Open Server Socket...\n")  
  12.     AFUNIXServerSocket server = AFUNIXServerSocket.newInstance()  
  13.     server.bind(new AFUNIXSocketAddress(socketFile))  
  14.     socketReady=true  
  15.     Socket socket = server.accept()  
  16.     try  
  17.     {  
  18.         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))  
  19.         bw.writeLine("Hello World!")  
  20.         bw.close()  
  21.     }  
  22.     catch(Exception e)  
  23.     {  
  24.         e.printStackTrace()  
  25.     }  
  26.     finally  
  27.     {  
  28.         socket.close()  
  29.     }  
  30.     sleep 1000  
  31.     printf("\t[Info] Close Server Socket...\n")  
  32.     server.close()  
  33. }  
  34.   
  35. // Client Socket  
  36. while(!socketReady) sleep(1000)  
  37. printf("\t[Info] Open Client Socket...\n")  
  38. AFUNIXSocket sock = AFUNIXSocket.newInstance();  
  39. sock.connect(new AFUNIXSocketAddress(socketFile));  
  40. BufferedReader input =new BufferedReader(new InputStreamReader(sock.getInputStream()))  
  41. String line=null  
  42. printf("\t[Info] Read from Socket:\n")  
  43. while((line=input.readLine())!=null)  
  44. {  
  45.     printf("\t\t%s\n", line)  
  46. }  
  47. printf("\t[Info] Close Client Socket...\n")  
  48. sock.close()  
Complete Examples 
junixsocket provides working demos for: 
plain Unix Domain Socket client/server communication and for
RMI-over-Unix-Socket.
MySQL-over-Unix-Socket.

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 SocketPlease 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

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...