2011年1月13日 星期四

Run jnlp program as root (sudo)

In Linux, a non-root user usually clicks a jnlp link in a web browser or double-clicks a jnlp file to run a Java Web Start program.
The program has no root privilege.

How to run it as root ?
-> sudo javaws http://your-website/link.jnlp

or download the jnlp file and
-> sudo javaws ~/Test/link.jnlp

Reference:

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:

2010年5月11日 星期二

[socket] set read/write timeout values for blocking socket

Set timeout to 10 seconds when doing read () or write () a blocking sock :

#define SOCK_RCV_TIMEOUT 10
#define SOCK_SND_TIMEOUT 10

void set_rw_timeout (int sock, int clear_timeout)
{
struct timeval rw_timeout;

memset (&rw_timeout, 0, sizeof (struct timeval));
rw_timeout.tv_sec = clear_timeout ? 0 : SOCK_RCV_TIMEOUT;

if (setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, &rw_timeout, sizeof (struct timeval))) {
fprintf (stderr, "setsockopt SO_RCVTIMEO failed\n");
}

memset (&rw_timeout, 0, sizeof (struct timeval));
rw_timeout.tv_sec = clear_timeout ? 0 : SOCK_SND_TIMEOUT;

if (setsockopt (sock, SOL_SOCKET, SO_SNDTIMEO, &rw_timeout, sizeof (struct timeval))) {
fprintf (stderr, "setsockopt SO_SNDTIMEO failed\n");
}
}

2010年4月26日 星期一

md5sum a directory

Generate:
find
(your-dir) -type f -print0 | xargs -0 md5sum >> (output-md5sume-file)

Verify:
md5sum -c (output-md5sum-file)


my example:

robert@robert-desktop ~/work/nbd-work/ttt $ ls
first second test

robert@robert-desktop ~/work/nbd-work/ttt $ find -type f -print0 | xargs -0 md5sum >> ~/Test/mydir.md5


robert@robert-desktop ~/work/nbd-work/ttt $ cat ~/Test/mydir.md5
d41d8cd98f00b204e9800998ecf8427e ./second
d41d8cd98f00b204e9800998ecf8427e ./first
7423d8fc12da81d779d0485b3d3fb67d ./test

robert@robert-desktop ~/work/nbd-work/ttt $ md5sum -c ~/Test/mydir.md5
./second: OK
./first: OK
./test: OK

robert@robert-desktop ~/work/nbd-work/ttt $ echo $?
0



2010年4月13日 星期二

Use djgpp to build a DOS program in dosemu (under Linux)

Topic: How to use djgpp to build a DOS program in DOSEmu (running in Linux) ?

1. Install dosemu

DOSEmu (dosemu HOWTO)

2. Download djgpp files for dosemu

DJGPP (this name comes from ... DJ's GNU Programming Platform)

Use DJGPP Zip File Picker to download what you need (select dosemu for Operating System)

3. Install djgpp into dosemu

Refer to "installation instructions for dosemu" shown in DJGPP Zip File Picker Results. Key points:

* unzip those *.zip files to ~/djgpp/ (~ is your home in Linux)
* edit ~/.dosemu/drvie_c/autoexec.bat :
set PATH=d:\djgpp\BIN;%PATH%
set DJGPP=d:\djgpp\DJGPP.ENV
(dosemu defaults D:\ to ~/)
* reboot dosemu

4. Compile a helloworld in dosemu
* Edit main.c and its Makefile in ~/helloworld/ under Linux
* Then in dosbox, cd D:\helloworld\ and try to make it !
(gcc -o helloworld.exe main.c)

2010年4月7日 星期三

stop on first error (gcc, make)

Just want to see the first compilation error ? Try this:

-> gcc main.c -Wfatal-errors


-Wfatal-errors
This option causes the compiler to abort compilation on the first
error occurred rather than trying to keep going and printing
further error messages.

Ref:
man gcc