[ Prev ][ Table of Contents ][ Front Page ][ Talkback ][ FAQ ][ Next ]
LINUX GAZETTE
...making Linux just a little more fun!
A Keep-Alive Program You Can Run Anywhere
By Graham Jenkins

The Poodle and the Labrador

You are halfway through typing a new program into a remote machine connected over a dial-up line, and you get called to intervene in a fight between your partner's miniature poodle and the neighbour's ugly yellow Labrador. When you get back, your connection has timed-out.

Is this something that has happened to you? Or perhaps you had to drag your kids away from a particularly offensive episode of Jerry Springer, then found you had to stick around to make sure they didn't come back?

The Blonde Solution

The traditional procedure for maintaining activity on your line during an interruption of the type outlined above was to use a 'fortune' program in a small loop so that a random saying got written to your screen every half-minute. This could present some real problems if a person with fair hair looked at your screen and saw something like:

 Q:  How do you make a blonde's eyes light up?
 A:  Shine a flashlight in her ear.

You could of course used a '-i' or equivalent parameter restricting 'fortune' to generating inoffensive material like:

  Computing Definition
  Chaining - A method of attaching programmers to desk, to speed up output.

The more recent incarnations of the 'fortune' program offer their users a more specific set of options. You can chose between offending those who are Irish, gay or intellectually retarded!

For The Terminally-Challenged

If you are just using a browser to read your Hotmail messages, you probably won't want to open a terminal window just so you can run a 'fortune' program. If you are using an X11-compliant window-manager, you could start a clock program with something like:

 xclock -digital -update 1 &

But that's not going to work on your your vintage Windows 95 machine unless you also happen to be running something like PC-Xware.

The 'KeepAlive.java' program listed here is designed to work anywhere. It's written in Java 1.1 so that even the 'jview' virtual machine on your basic Microsoft machine can handle it. It doesn't rely on finding a 'fortune', 'xclock' or other program on a remote machine. And you don't have to change anything when you connect via a different ISP.

Finding A Partner

But you have to send traffic somewhere, right? So how do you find a partner machine which will receive your traffic? If we were writing this program as a shell script, we might work out where our gateway was, and ping it at appropriate intervals. That's not so easy to do in a Java program which might run on any number of platforms. And in any case, it would be nice if we could send traffic somewhere beyond the gateway machine.

In almost every sort of networking arrangement, the participating machines have knowledge of one or more nameserver addresses. So what we can do from our Java program is make periodic requests to those nameservers. We need to ensure that any hosts whose addresses we request cannot be found locally in a hosts table. And we need to also ensure that the answers to our nameserver requests are not cached locally. If you take a look now at the program, you will see that the names of the hosts whose addresses we are requesting are generated by examining the clock-time in milliseconds at the time of each request. This results in names like A1040689223909, A1040689229448, etc.

That's really all we need to do. But it's nice to be able to see something happening. So our program defines a 'MessageFrame' class which displays two colored buttons in a GUI window. The colors of these are changed at each iteration. We also set the title on the GUI window, and change it at each iteration - so we can still see something happening when the window is minimized. And we set up a listener to detect 'window closing' events and perform a graceful shutdown.

Getting It Together

Here's the program. You need to compile it with a command like:

 javac KeepAlive.java
This will generate three 'class' files which contain code which can be executed on a java virtual machine. So you can copy those class files to a directory on another machine, then execute it with a command like:
 java KeepAlive
To use the Microsoft virtual machine on a Windows box, use:
 java KeepAlive

/* @(#) KeepAlive.java  Trivial keep-alive program. Tries at 5-second intervals
 *                      to find addresses for hosts with generated names. This
 *                      ensures that messages are sent to nameserver(s).
 *                      Copyright (c) 2002 Graham Jenkins <grahjenk@au1.ibm.com>
 *                      All rights reserved. Version 1.06, August 15, 2002.
 */
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
public class KeepAlive {
  public static void main(String[] args) {
    MessageFrame f=new MessageFrame();  // Change button colours each iteration.
    int flag=0;                         // Also switch frame-title so we can see
    while ( true ) {                    // activity whilst iconified.
      f.statusMess(Color.red,Color.red); f.setTitle("==X==");
      try {InetAddress addr=InetAddress.getByName("A"+(new Date()).getTime());}
      catch (UnknownHostException ioe) {}
      if(flag==0) {f.statusMess(Color.yellow,Color.green); f.setTitle("1.06");}
      else {f.statusMess(Color.green,Color.yellow); f.setTitle("KeepAlive");}
      flag=1-flag;
      try {Thread.sleep(5000L);} catch (InterruptedException e) {}
    }
  }
}

class MessageFrame extends Frame implements ActionListener {
  private Button b1, b2;                // Displays two coloured buttons.
  public MessageFrame() {
    Panel p=new Panel(); p.setLayout(new FlowLayout());
    b1=new Button() ; b2=new Button(); p.add(b1); p.add(b2);
    this.add("South",p); this.setSize(150,50); this.show();
    this.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) { System.exit(0); }
    });
  }
  public void statusMess(Color left, Color right) {
    b1.setBackground(left); b2.setBackground(right);
  }
  public void actionPerformed(ActionEvent e) {}
}

If you have Java 1.1 or later, and no requirement to use the Microsoft virtual machine, you can assemble the class files into a single 'jar' file, then execute it using the '-jar' option thus:

  echo "Main-Class: KeepAlive\015" >/tmp/MyManifest
  jar cmf /tmp/MyManifest /tmp/KeepAlive.jar *.class
  
  java -jar /tmp/KeepAlive.jar

If You Don't Have It

If your machine doesn't have Java, you can get it from Sun MicroSystems. And if you need to know more about network programming with Java, you could take a look at "Java Network Programming and Distributed Computing" by David Reilly and Michael Reilly.

 

[BIO] Graham is a Unix Specialist at IBM Global Services, Australia. He lives in Melbourne and has built and managed many flavors of proprietary and open systems on several hardware platforms.


Copyright © 2003, Graham Jenkins. Copying license https://www.linuxgazette.net/copying.html
Published in Issue 87 of Linux Gazette, February 2003

[ Prev ][ Table of Contents ][ Front Page ][ Talkback ][ FAQ ][ Next ]