Below is the code I'm working on. The code will execute a Swing-based GUI, with a simple JFrame that holds a button which when pressed will run the Ping utility in the background. If the host is either reachable or not, a dialog will be displayed with a successful/unsuccess message.
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.InetAddress;
public class App {
private JTextArea clickThisButtonTextArea;
public JButton button1;
private JPanel panelMain;
public App() {
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
try {
sendPing("8.8.8.8");
//JOptionPane.showMessageDialog(null, sendPing("192.168.0.1"));
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public String sendPing(String ipAddr) throws IOException {
InetAddress ip = InetAddress.getByName(ipAddr);
boolean ipReach = ip.isReachable(5000);
System.out.println("Sending Ping Request to " + ipAddr);
if (ip.isReachable(5000)) {
JOptionPane.showMessageDialog(null, "Host is reachable!");
System.out.println("Host is reachable");
} else {
JOptionPane.showMessageDialog(null, "Sorry, no host!");
System.out.println("Sorry ! We can't reach to this host");
}
return null;
}
public static void main(String[] args) {
JFrame frame = new JFrame("App");
frame.setContentPane(new App().panelMain);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setSize(250,250);
}
}
The image below shows a tcpdump of traffic flowing out after the button is pressed vs. a ping ran from the terminal.
Any insights will be greatly appreciated.