简体   繁体   中英

how can I identify machines on home network using Java

I'm trying to write a Java program to find the address of any machines connected to my home network. My first idea is to go through each address in the range 192.168.1.[0-254] and check if there is a machine on the network with that address, but I cant figure out a simple way to check for a given address whether there is a machine with that address. I guess I could try opening a socket on every possible port and conclude that if they all fail then there is no machine, but at a first glimpse this seems to take far too long when there is no machine (it took over a minute to check just 2 ports on an address with no machine attached).

Is there an quick and easy way to check if there is a machine with given address? Is there a better way to do this in Java besides just trying every address?

Thanks in advance for any help

If you just want to find out which IPs on your network are taken, just use nmap .

If you want to learn how to do this for yourself, I might suggest first doing the job in C. All the sockets APIs are written in C, and it is perhaps the most natural environment for working with sockets. (Plus, TCP/IP Illustrated uses C, so examples are easier to find.)

That said, there are examples of Java using ICMP ; they'll all be using jnetpcap or Jpcap , wrappers around libpcap (the underlying C library).

If you want to do port-scanning, you could take several approaches:

  • You could multi-thread your application: create a thread-pool of 100 or 200 threads, and submit 'jobs' to attempt connect() to each (IP, Port) pair that might be open. Since there are 65k ports per IP and connection timeouts can be quite long (if your code doesn't get an RST or ICMP Host or Port unreachable response), this can be very slow and possibly very memory intensive. You could set an alarm in each thread if the connection isn't established in three or five seconds...

  • You could swap to a lower level and use libpcap or similar tools to send hand-crafted connection attempts, and only note the replies (and don't bother to set up the connections). This is one of the approaches taken by nmap , and one of the reasons why it gives answers much more quickly than your application.

You could try issuing a ping command using Runtime.exec or develop a ping client within java itself. Going with ping would save you from having to check every port.

You could use InetAddress.isReachable(int timeout) since that sends a ICMP PING, but there are a few permission gotchas using that ( search SO for help with the particulars).

You can also send out UDP packets using DatagramSocket and look for PortUnreachableException s. If you send them to port 7 (echo) you should either get an exception or the same data you sent out as a response. This method also has some gotchas since it requires all hosts on the network to send a response when it receives a UDP packet on a port it is not listening to. Usually they do, but firewalls might filter it out.

Just running the ping command might be the simplest way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM