Link to home
Start Free TrialLog in
Avatar of shijuh
shijuh

asked on

Check a Local IP to have http protocol

How do i check a system in the LAN is  supporting http?
I often change my LAN settings(Internet options in IE
) to different machines depending  upon the availability .
It is difficult for me to find which one is up.
Other wise i have to have the trail and error. If I have a program
where i can list all my IP's  that displays the http status that
is easier for me to set the LAN settings...

Avatar of Mig-O
Mig-O

The following small program checks all addresses in you local subnets for Servers. Massively Multi-Threaded, btw. Enjoy.

---------------------

import java.net.*;
import java.util.*;

public class SearchHTTPServers {
   
    public static final ThreadGroup g = new ThreadGroup("Search Threads");
   
    public static void main(String[] args) throws Exception {
        Enumeration interfacesEnum =
            NetworkInterface.getNetworkInterfaces();
        while( interfacesEnum.hasMoreElements() ) {
            Enumeration inetAddressEnum =
                ((NetworkInterface)interfacesEnum.nextElement()).
                    getInetAddresses();
            while( inetAddressEnum.hasMoreElements() ) {
                InetAddress inetAddress = (InetAddress)inetAddressEnum.
                    nextElement();
                checkSubnet(inetAddress);
            }
        }
        while( true ) {
            if(g.activeCount()==0) {
                System.out.println("done.");
                break;
            }
            Thread.sleep(100);
        }
    }
   
    public static void checkSubnet(InetAddress inetAddress) throws Exception {
        byte[] address = inetAddress.getAddress();
        for( int i=1; i<255; i++ ) {
            String ip = (((int)address[0])+256)%256+"."+
                        (((int)address[1])+256)%256+"."+
                        (((int)address[2])+256)%256+"."+
                        i;
            if( !ip.startsWith("127.0.0") ) {
                new Thread( g,new CheckThread(ip) ).start();
            }    
        }  
    }
   
    static class CheckThread implements Runnable {
        String ip;
        public CheckThread(String ip) {
            this.ip = ip;
        }
        public void run() {
            try {
                URL url = new URL("http",ip,80,"/");
                url.openStream();
                System.out.println(ip+" is up");
            } catch( Exception e ) {
                //System.out.println(ip+" is down");
            }
        }
    }
   
}


Avatar of shijuh

ASKER

Mig-O,
The logic seems to be OK !
It is therowing an error in
(NetworkInterface)interfacesEnum.nextElement()).
is it due the the lack of NetworkInterface class?

Shiju
I tried it with j2sdk1.4.1. A look into the documentation states, "NetworkInterface" exists since 1.4, so i believe you have 1.3 or lower on your machine.

An easier soultion would be to replace the Enumeration-while-stuff in main() with:

checkSubnet(InetAddress.getLocalHost()), which is available since 1.0.

cheers,
Daniel Migowski

Btw. a look into the documentation says:

public static Enumeration getNetworkInterfaces() throws SocketException

Returns all the interfaces on this machine. Returns null if no network interfaces could be found on this machine.
Avatar of shijuh

ASKER

Daniel,
I have installed 1.4 and it is working now ...
But the output is not as par with my expectation !
The program is listing the computers having port 80.
I am connected in LAN. My internet access is through a
proxy server. I set this address and port in internet options. I want to know that is up and capable of
accepting http requests
Shiju

Add a loop for common proxyports to the code.
ASKER CERTIFIED SOLUTION
Avatar of Mig-O
Mig-O

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shijuh

ASKER

Would you please gimme the entire code . I am not that
familiar with java.I am compile, run  and do small
modifications...
Shiju
shijuh:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.