• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

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...

0
shijuh
Asked:
shijuh
  • 5
  • 3
1 Solution
 
Mig-OCommented:
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");
            }
        }
    }
   
}


0
 
shijuhAuthor Commented:
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
0
 
Mig-OCommented:
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

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Mig-OCommented:
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.
0
 
shijuhAuthor Commented:
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

0
 
Mig-OCommented:
Add a loop for common proxyports to the code.
0
 
Mig-OCommented:
Okey, i didn't understand before. You already know, which machines might be up.

Here you see how to set a proxy:

String url = "http://www.marchal.com/",
       proxy = "proxy.mydomain.com",
       port = "8080";
URL server = new URL(url);
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost",proxy);
systemProperties.setProperty("http.proxyPort",port);
HttpURLConnection connection = (
    HttpURLConnection)server.openConnection();
connection.connect();
InputStream in = connection.getInputStream();
readResponse(in);

Now code something yourself, using the example, and check one after another, which of your selected machines are up.
0
 
shijuhAuthor Commented:
Would you please gimme the entire code . I am not that
familiar with java.I am compile, run  and do small
modifications...
Shiju
0
 
CleanupPingCommented:
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now