IP address of remote device in a LAN using Java

i have written a code that should give me the ip addresses of all the devices connected in a wireless network.But it is just showing the ip address of the default gateway router and my own system. so plz can someone help me with this.

importjava.io.IOException;
importjava.net.InetAddress;
 
publicclass NetworkPing {
 
 
publicstaticvoid main(String[] args)throws IOException {
 
InetAddress localhost = InetAddress.getLocalHost();
// this code assumes IPv4 is used
byte[] ip = localhost.getAddress();
 
for(int i =1; i <=254; i++)
{
ip[3]=(byte)i;
InetAddress address = InetAddress.getByAddress(ip);
if(address.isReachable(1000))
{
System.out.println(address +" machine is turned on and can be pinged");
}
elseif(!address.getHostAddress().equals(address.getHostName()))
{
System.out.println(address +" machine is known in a DNS lookup");
}
else
{
System.out.println(address +" the host address and host name are equal, meaning the host name could not be resolved");
}
}
 
}
}

Replies

  • Kaustubh Katdare
    Kaustubh Katdare
    I added code tags, and it seems to have messed up the entire formatting. Could please post your code again so that I can make it appear properly?

    Update: Fixed! 😀
  • durga ch
    durga ch
    I am a java illeterate but as I read from your code, loks like you are trying to reach all IPs 1.1.1.1 to 255.255.255.255 and see whch ones are reachable and then returning the ip ??
    if your logic is different let me know.

    can I know the networj setup where you are using this script???
  • KenJackson
    KenJackson
    I think isReachable() uses raw sockets, which requires root (or Administrator) priviledge.

    BTW, if you just want the answer and aren't specifically trying to write a program, the Nmap: the Network Mapper - Free Security Scanner program does a very good job.

You are reading an archived discussion.

Related Posts

I'm interested to appear for IES exam.We may have this discussion on IES exams.People who wants to write this service exam can do discussion here.if you even don't have idea...
hi, I get a message that keeps on popping asking me to 'reconsider replacing the battery'. This perhaps means that my battery is not able to hold extra charge for...
Well I never knew this existed till this day and the moment I found it I wanted to share it to CEans Do check the links of how they are...
Use all the basic arithmetic operators to get 24 This is easy But you must use these numbers alone and get it Try to get 24 using 8,3,3 and an...
It’s fun, It’s entertaining, & It’s back!!! That’s right….. ASME GIKI Chapter (Pakistan) is organizing one of the most anticipated events of the year the 2nd International Mechanical Engineering Convention...