My LAN is:
- 192.168.1.100 - printer
- 192.168.1.101 - pc-mac
- 192.168.1.102 - pc-win
- 192.168.1.254 - router
I would list all ip in a jList.
My code is:
public void listIP(){
try
{
InetAddress address;
DefaultListModel iplan = new DefaultListModel();
jList1.setModel(iplan);
InetAddress localhost = InetAddress.getLocalHost();
iplan.addElement(localhost.getHostAddress());
String ip;
ip = localhost.getHostAddress();
String currIP;
for (int i = 0; i < 256; i++)
{
try
{
currIP = ip.substring(0,ip.length()-3)+String.valueOf(i);
address = InetAddress.getByName(currIP);
if (address.isReachable(50))
{
iplan.addElement(address.getHostAddress());
}
}
catch (IOException e) {
ex.PrintStackTrace();
}
}
}
catch (UnknownHostException ex) {
ex.PrintStackTrace();
}
but the output is only:
192.168.1.100
192.168.1.101
while i can ping:
ping -t 3 192.168.1.102
PING 192.168.1.102 (192.168.1.102): 56 data bytes
64 bytes from 192.168.1.102: icmp_seq=0 ttl=128 time=2.936 ms
64 bytes from 192.168.1.102: icmp_seq=1 ttl=128 time=11.346 ms
64 bytes from 192.168.1.102: icmp_seq=2 ttl=128 time=2.913 ms
--- 192.168.1.102 ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 2.913/5.732/11.346/3.970 ms
and
ping -t 3 192.168.1.254
PING 192.168.1.254 (192.168.1.254): 56 data bytes
64 bytes from 192.168.1.254: icmp_seq=0 ttl=254 time=2.094 ms
64 bytes from 192.168.1.254: icmp_seq=1 ttl=254 time=2.316 ms
64 bytes from 192.168.1.254: icmp_seq=2 ttl=254 time=2.228 ms
--- 192.168.1.254 ping statistics ---
4 packets transmitted, 3 packets received, 25.0% packet loss
round-trip min/avg/max/stddev = 2.094/2.213/2.316/0.091 ms
I can ping all ip but it show only two. Why? How can i fix it? Any ideas?