A Keep-Alive Program You Can Run Anywhere
The following article is taken from the February issue of our on-line sister publication, Linux Gazette.
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 the kids didn't come back?
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 a random saying was 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.
Of course, you could have used an -i or equivalent parameter to restrict fortune from generating inoffensive material. The more recent incarnations of the fortune program offer their users a more specific set of options.
If you only use a browser to read your Hotmail messages, you probably won't want to open a terminal window 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.
On the other hand, the KeepAlive.java program, presented below, is designed to work anywhere. It's written in Java 1.1, so even the jview virtual machine on a basic Microsoft machine can handle it. It doesn't rely on finding fortune, xclock or any other program on a remote machine. And you don't have to change anything when you connect via a different ISP.
But to use this, you have to send traffic somewhere, right? So how do you find a partner machine that 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. 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 name server addresses. So what we can do from our Java program is make periodic requests to those name servers. We need to ensure that any hosts whose addresses we request cannot be found locally in a hosts table. And we also need to ensure that the answers to our name server requests are not cached locally. If you take a look at the program below, you can 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 approach results in names like A1040689223909, A1040689229448 and so on.
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 that 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; this way, we still can see something happening when the window is minimized. And we set up a listener to detect window-closing events and to perform a graceful shutdown.
The script is available in Listing 1.
You need to compile it with a command like:
javac KeepAlive.java
This command generates three class files containing code that can be executed on a Java virtual machine. Therefore, you can copy those class files to a directory on another machine, then execute them 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, like this:
echo "Main-Class: KeepAlive\015" >/tmp/MyManifest jar cmf /tmp/MyManifest /tmp/KeepAlive.jar *.class java -jar /tmp/KeepAlive.jar
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Joomla Installation Service
11 hours 43 min ago - features
17 hours 2 min ago - Reply to comment | Linux Journal
1 day 3 hours ago - Nice article, thanks for the
1 day 14 hours ago - I once had a better way I
1 day 20 hours ago - Not only you I too assumed
1 day 20 hours ago - another very interesting
1 day 22 hours ago - Reply to comment | Linux Journal
2 days 12 min ago - Reply to comment | Linux Journal
2 days 7 hours ago - Reply to comment | Linux Journal
2 days 7 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
Re: A Keep-Alive Program You Can Run Anywhere
It seems like poor internet citizenship to bother a name server with worthless requests.
Re: A Keep-Alive Program You Can Run Anywhere
ping is too difficult to use?
Re: A Keep-Alive Program You Can Run Anywhere
As it is a use anywhere app, it can be used on windows which doesn't have an inter packet timing option. So to not bombard a server with icmp requests its this or a ping application.
As my firewall blocks icmp I find this useful.
Re: A Keep-Alive Program You Can Run Anywhere
Java can't generate ICMP packets, you must use a C dll using JNI but ... where is portability ???? ;-)