isReachable

Hi,

I am using isReachable to ping and check whether the ip address is already in use or not. I've no problem implementing the function in Windows. However, when I tried to run the same program in Linux(fedora 5) even after setting the setuid bit for java,

http://amazing-development.com/archives/2004/12/20/send-icmp-ping-in-java-15/

the program simply freeze. The isReachable never gets back a response of true or false, and the there are no errors or exceptions. I can't seem to find any other thread describing the same problem. My guess is that probably some of my setting is not set correctly. The program is running on java 1.6 and I post the code below for reference.publicclass JspinnerTestextends JDialog{

private JPanel contentPane;

private JTextField textField1;

private JButton searchPingableButton;

private JButton buttonOK;

/**

* Number of bytes in an ip address

*/

publicstaticfinalint MAX_SIZE = 4;

public JspinnerTest(){

setContentPane(contentPane);

setModal(true);

getRootPane().setDefaultButton(buttonOK);

searchPingableButton.addActionListener(new ActionListener(){

/**

* Invoked when an action occurs.

*/

publicvoid actionPerformed(ActionEvent e){

Pinger();

}

});

}

publicstaticvoid main(String[] args){

JspinnerTest dialog =new JspinnerTest();

dialog.pack();

dialog.setSize(100, 100);

dialog.setVisible(true);

System.exit(0);

}

privatevoid Pinger(){

Enumeration interfaces =null;

InetAddress ipAddress =null;

boolean isReachable =false;

try{

ipAddress = InetAddress.getByName(textField1.getText());

}catch (UnknownHostException e){

e.printStackTrace();//To change body of catch statement use File | Settings | File Templates.

}

try{

interfaces = NetworkInterface.getNetworkInterfaces();

}catch (SocketException e){

e.printStackTrace();//To change body of catch statement use File | Settings | File Templates.

}

assert interfaces !=null;

while (interfaces.hasMoreElements()){

NetworkInterface nextElement = (NetworkInterface) interfaces.nextElement();

try{

assert ipAddress !=null;

isReachable = ipAddress.isReachable(nextElement, 10, 5000);

}catch (IOException e){

e.printStackTrace();//To change body of catch statement use File | Settings | File Templates.

}

if (isReachable){

System.out.println("Reached ipAddress" + ipAddress.getHostAddress() +"in interfaces" + nextElement.getDisplayName());

}

}

}

privatevoid createUIComponents(){

}

{

// GUI initializer generated by IntelliJ IDEA GUI Designer

// >>> IMPORTANT!! <<<

// DO NOT EDIT OR ADD ANY CODE HERE!

$$$setupUI$$$();

}

/**

* Method generated by IntelliJ IDEA GUI Designer

* >>> IMPORTANT!! <<<

* DO NOT edit this method OR call it in your code!

*

* @noinspection ALL

*/

privatevoid $$$setupUI$$$(){

contentPane =new JPanel();

contentPane.setLayout(new GridBagLayout());

final JPanel panel1 =new JPanel();

panel1.setLayout(new GridBagLayout());

GridBagConstraints gbc;

gbc =new GridBagConstraints();

gbc.gridx = 0;

gbc.gridy = 0;

gbc.weightx = 1.0;

gbc.weighty = 1.0;

gbc.fill = GridBagConstraints.BOTH;

contentPane.add(panel1, gbc);

final JPanel spacer1 =new JPanel();

gbc =new GridBagConstraints();

gbc.gridx = 1;

gbc.gridy = 1;

gbc.fill = GridBagConstraints.HORIZONTAL;

panel1.add(spacer1, gbc);

textField1 =new JTextField();

gbc =new GridBagConstraints();

gbc.gridx = 0;

gbc.gridy = 0;

gbc.weightx = 1.0;

gbc.anchor = GridBagConstraints.WEST;

gbc.fill = GridBagConstraints.HORIZONTAL;

panel1.add(textField1, gbc);

searchPingableButton =new JButton();

searchPingableButton.setText("Search Pingable");

gbc =new GridBagConstraints();

gbc.gridx = 0;

gbc.gridy = 2;

gbc.fill = GridBagConstraints.HORIZONTAL;

panel1.add(searchPingableButton, gbc);

}

/**

* @noinspection ALL

*/

public JComponent $$$getRootComponent$$$(){return contentPane;}

}

Thanks

[7774 byte] By [annikkakittya] at [2007-11-26 20:30:59]
# 1

> However, when I tried to run the same program in

> Linux(fedora 5) even after setting the setuid bit

> for java,

*gasp* Yikes... don't ever do that on any machine on any network that means anything :).

Try looking in this thread:

http://forum.java.sun.com/thread.jspa?threadID=473589&messageID=2193039

It's old and all, but it looks like there's not much of an alternative except by opening your own socket, or "Runtime.exec()"-ing it.

kevjavaa at 2007-7-10 1:20:42 > top of Java-index,Java Essentials,Java Programming...
# 2

Why are you repeating the same InetAddress.isReachable() call for every NetworkInterface but without getting any data from the NetworkInterface?

You are executing the isReachable() call in the AWT thread. Don't. Start a separate thread for any network I/O.

I'm also not clear what the purpose is. If you want to communicate with a host and you're not sure if it's there, just try the connect and handle the exception.

ejpa at 2007-7-10 1:20:43 > top of Java-index,Java Essentials,Java Programming...
# 3

Thank you keyjava for the Runtime suggestion, I didn't really think of that yet. As for the latest comment, the code I pasted out is just a short sample for debugging usage. The final code will not be run in the awt. The isReachable is called and implemented to all networkInterface to ensure that ipaddress that the user is planning to use has not been used. I had managed somehow to get the code running in Fedora 6 without setting the suid. I am still not sure why the other computer running in Fedora5 doesn't work, but thank you all for the suggestions:)

annikkakittya at 2007-7-10 1:20:43 > top of Java-index,Java Essentials,Java Programming...
# 4

But the isReachable call has nothing to do with the network interface. You aren't getting the ip address of the network interface and seeing if that is reachable. And if you did you would find that any IP address on a local network interface is reachable by definition. So you're just repeating the same isReachable() call with the same data N times where N is the number of local interfaces.

So what is the point?

and what exactly do you mean by 'the ip address the user is planning to use'?

Have you heard of DHCP?

ejpa at 2007-7-10 1:20:43 > top of Java-index,Java Essentials,Java Programming...