Listing 1. KeepAlive.java
/* @(#) 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
* 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) {}
}