Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm a java beginner and I would like help to solve my problem. I want to use java language to detect an access point and display the mac address of the access point. I have 3 classes and can only detect ip address and mac address of a host. Below is my code.

Java
head.java
public class head
{
    public static void main(String[] args) throws InterruptedException
    {
        String tracerserver="http://127.0.0.1/eds/track.php";//tracker server address
        int timeRange=1;//in minutes

        System.out.println("Application running now...!");

        while(true)
        {
            try
            {
                tracer machine = new tracer();
                //System.out.println("Ip address: "+machine.ipaddr());
                //System.out.println("Mac Address: "+machine.macAddr());

                String requestParameters ="ip="+machine.ipaddr()+"&mac="+machine.macAddr()+"&point=3.223,2.93";
                sender.sendGetRequest(tracerserver, requestParameters);

                Thread.sleep((timeRange*60000));
            }
            catch(Throwable t)
            {
                System.err.println("Error:"+t);
            }
        }
    }
}

sender.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;
public class sender
{
    public sender(){}

    public static String sendGetRequest(String endpoint, String requestParameters)
    {
        String result = null;
        if (endpoint.startsWith("http://"))
        {
            //Send a GET request to the servlet
            try
            {
                String urlStr = endpoint;
                if (requestParameters != null && requestParameters.length () > 0)
                {
                    urlStr += "?" + requestParameters;
                }
                URL url = new URL(urlStr);
                URLConnection conn = url.openConnection ();
                // Get the response
                BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuffer sb = new StringBuffer();
                String line;
                while ((line = rd.readLine()) != null)
                {
                    sb.append(line);
                }
                rd.close();
                result = sb.toString();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return result;
    }
}

tracer.java
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;
public class tracer
{
    public tracer(){}
    public String ipaddr()
    {
        try
        {
            InetAddress localaddr = InetAddress.getLocalHost();
            return  localaddr.getHostAddress();
        }
        catch(UnknownHostException e)
        {
            return "Can't detect localhost : " + e;
        }
    }

    public String macAddr()
    {
        try
        {
            InetAddress address = InetAddress.getLocalHost();
            NetworkInterface ni = NetworkInterface.getByInetAddress(address);
            if (ni != null)
            {
                byte[] mac = ni.getHardwareAddress();
                if (mac != null)
                {
                    String aa = HexBin.encode(mac) ;
                    String ab = "";
                    int a=0;
                    int b=2;

                    for (int i = 0; i < mac.length; i++)
                    {
                        ab+=aa.substring(a, b)+ ((i < mac.length - 1) ? "-" : "") ;
                        a=a+2;
                        b=b+2;
                    }
                    return ab;
                }
                else
                {
                    return "Address doesn't exist or is not accessible.";
                }
            }
            else
            {
                return "Network Interface for the specified address is not found.";
            }
        }
        catch (UnknownHostException e)
        {
            e.printStackTrace();
        }
        catch (SocketException e)
        {
            e.printStackTrace();
        }
        return null;
    }
}


Hope that somebody help me in this problem.
Posted
Updated 22-Apr-11 7:52am
v2
Comments
Tarakeshwar Reddy 22-Apr-11 13:52pm    
Added pre tags and modified the text

I suppose you could do a tracert and retrieve the IP address from the first reported hop in the list.
 
Share this answer
 
XML
Sir if you have done with it. kindly send me a code to my email id. i have tried but couldnt able to do it.
my email id is <b>sana.asghar928@gmail.com</b>

Sana Asghar
thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900