Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doesn't list all LAN IP  RSS feed

 
Richie Davies
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

My code is:
but the output is only:

while i can ping:

and
Why?
How can I fix it?
 
Tim Moores
Saloon Keeper
Posts: 3902
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that will only list hosts which your computer has recently connected to. Try opening a TCP connection to them first.
 
Richie Davies
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote: Try opening a TCP connection to them first.

How? What is the java code?
 
Richie Davies
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please can somebody help me?
 
Ron McLeod
Saloon Keeper
Posts: 1601
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most likely InetAddress#isReachable is using TCP echo rather than ICMP echo to try to detect the hosts:

Test whether that address is reachable. Best effort is made by the implementation to try to reach the host, but firewalls and server configuration may block requests resulting in a unreachable status while some specific ports may be accessible. A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host.

TCP echo will not enabled by many of the devices that you may probe.  It needs to be explictly be installed as a service in Windows, and configured on in Linux.  You could try running ping with something like this. but it would be very slow for your application if kept single-threaded - Windows seems to have a minimum timeout of 1 second:

 
Ron McLeod
Saloon Keeper
Posts: 1601
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To test using the sample code, replace
with
 
Richie Davies
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for reply. I'll try and I'll inform you.
 
Richie Davies
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved by this function:


but in a loop from to 1 to 254 hosts, it takes 44 min. It's too much.
How can it take less time?
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One warning about your process handling: I suggest you read When Runtime.exec() won't. It's an old article, but still valid. In your case, you're not reading any possible error output, so you could risk your programming hanging if a process' error buffer gets filled completely.
 
Richie Davies
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is not there a faster way to ping many hosts in java? Do somebody know it?
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't tried it, but perhaps http://www.icmp4j.org/d/index.html can help you out.
 
Tim Moores
Saloon Keeper
Posts: 3902
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richie Davies wrote:Why is not there a faster way to ping many hosts in java?

Somebody probably has written one, but not open-sourced it. You could fill that gap by writing code that accesses multiple hosts in parallel, and making it available to the general public.
 
Richie Davies
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:
Richie Davies wrote:Why is not there a faster way to ping many hosts in java?

Somebody probably has written one, but not open-sourced it. You could fill that gap by writing code that accesses multiple hosts in parallel, and making it available to the general public.

Can you help me to do it together?
 
Tim Moores
Saloon Keeper
Posts: 3902
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, I've done similar kind of stuff, so I'll let you go ahead. Seems a good case for the fork/join approach. Feel free to come back with questions, maybe in a new topic, possibly in the Concurrency forum.
 
Richie Davies
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok i found solution:

but i don' t understand why don't detect the IPs of localhost (192.168.1.101) and wifi router (192.168.1.254).
Any ideas?
 
Tim Moores
Saloon Keeper
Posts: 3902
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The javadocs of isReachable state:
A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host.

The actual implementation in the JDK is C code, though, which I didn't feel like delving into :-)

The reasons why those fail on localhost and the router need not be the same (and probably aren't), but you could try telnet-ing to port 7 of both hosts and see what happens, and also ping them manually.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!