2010年12月16日 星期四

JNLP and JNI native libraries

Topic: How to use JNLP to load jar files which include native libraries ?

1. Build native libraries
* HelloWorld.dll for Windows
* libHelloWorld.so for Linux (library name must be libXXX.so in Linux !!)

(How to build those native libraries ? see jni book)

2. Pack native libraries into jar

jar -cvf Linux32_lib.jar libHelloWorld.so
jar -cvf Win32_lib.jar HelloWorld.dll

3. Add tags in jnlp
(please replace ( with <)
(resources os="Windows">
(j2se version="1.5+"/>
(nativelib href="native/Win32_lib.jar"/>
(/resources>

(resources os="Linux">
(j2se version="1.5+"/>
(nativelib href="native/Linux32_lib.jar"/>
(/resources>

4. In code, explicitly load the library
System.loadLibrary("HelloWorld");
(windows->HelloWorld.dll ; linux -> libHelloWorld.so)

2010年12月2日 星期四

TCP Keepalive mechanism

RFC1122 says

"Implementors MAY include "keep-alives" in their TCP implementations, although this practice is not universally accepted. If keep-alives are included, the application MUST be able to turn them on or off for each TCP connection, and they MUST default to off." [1]


In C, use setsockopt() to set the following arguments on a specific socket [2]
* SO_KEEPALIVE: enable tcp keepalive
* TCP_KEEPCNT: overrides tcp_keepalive_probes
* TCP_KEEPIDLE: overrides tcp_keepalive_time
* TCP_KEEPINTVL: overrides tcp_keepalive_intvl

These settings are only for the current socket.

In Linux, to see the default values (OS-wide variables) of the later 3 options:
# cat /proc/sys/net/ipv4/tcp_keepalive_time
7200
# cat /proc/sys/net/ipv4/tcp_keepalive_intvl
75
# cat /proc/sys/net/ipv4/tcp_keepalive_probes
9

This means "wait for two hours (7200 secs) before sending the first keepalive probe, and then resend it every 75 seconds. If no ACK response is received for nine consecutive times, the connection is marked as broken." [2]


Ref: