participate


Socket Programming - MAC Address
<<   Back to Forum  |   Give us Feedback
This topic has 10 replies on 1 page.
thesimpsons
Posts:4
Registered: 4/9/05
MAC Address   
Apr 9, 2005 5:23 PM

 
Hi,
How do I display MAC address of client computer using socket programming. I was able to display the host name and IP address but I don't know how to display the MAC address. Also, how do I dsplay the time? Thanks.
 
sjasja
Posts:3,460
Registered: 2/15/99
Re: MAC Address   
Apr 9, 2005 6:14 PM (reply 1 of 10)  (In reply to original post )

 
Why?
 
sjasja
Posts:3,460
Registered: 2/15/99
Re: MAC Address   
Apr 9, 2005 6:27 PM (reply 2 of 10)  (In reply to #1 )

 
Oh, and the laptop I'm currently using has an ADSL IP address, a built-in WLAN adapter, a built-in Bluetooth adapter, a dialup PPP connection for emergencies (when ADSL is dead), a SLIP connection (though I've never used it), and a PCCARD net card I occasionally have inserted for ...uh, special purposes (let's not go there). What's my "the" MAC address, and why would I be interested in having your program display it?
 
tschodt
Posts:4,428
Registered: 4/24/98
Re: MAC Address   
Apr 10, 2005 3:03 AM (reply 3 of 10)  (In reply to original post )

 
Assuming you're talking Windows
Runtime.getRuntime().exec("cmd.exe -c ipconfig -all");
Needs some work, but you get the idea.
 
thesimpsons
Posts:4
Registered: 4/9/05
Re: MAC Address   
Apr 10, 2005 9:07 AM (reply 4 of 10)  (In reply to #3 )

 
Hi,
Thanks for your input. Is it possible to display MAC address without using runtime with ipconfig? I mean to display IP address I used: client.getInetAddress().getHostAddress(). Thanks again.
 
smithsa
Posts:97
Registered: 3/22/03
Re: MAC Address   
Apr 10, 2005 2:47 PM (reply 5 of 10)  (In reply to #4 )

 
I've done this before. Java has no native way of finding a MAC address, because there's really nothing you can do with it. If you really want it, then you need to do something platform specific.

What do you need a MAC address for anyway? We wanted it so we could identify a computer (for some stupid copy protection scheme). A MAC address is the only unique identifier that's guaranteed to be on a computer across all platforms. Of course, it can be cloned, and then your whole uniqueness scheme gets blown out of the water, but ...


Sander Smith
 
thesimpsons
Posts:4
Registered: 4/9/05
Re: MAC Address   
Apr 10, 2005 11:11 PM (reply 6 of 10)  (In reply to #5 )

 
Hi Sander Smith,

Thank you for your valuable input again. The reason I wanted to display the MAC address is because my professor wanted to display the MAC address "programmatically . I've searched this subject on the web but I haven't found any detail about this problem. I think the point is that he just want us to know whether this can be done in Java socket programming. Is there a way to do this with Visual C++? Thanks again for your input.

Toan Pham
 
BIJ001
Posts:6,899
Registered: 2003.06.06.
Re: MAC Address   
Apr 11, 2005 12:27 AM (reply 7 of 10)  (In reply to #6 )

 
The other endpoint of a socket can be behind routers, firewalls etc. A MAC address is lost and has no meaning behind the network segment.

So I am afraid it is inherently impossible to get the MAC address in the socket level.
 
thesimpsons
Posts:4
Registered: 4/9/05
Re: MAC Address   
Apr 11, 2005 11:00 AM (reply 8 of 10)  (In reply to #7 )

 
Thanks for your input. Is it possible to display the client time as soon as the client connected to the server? For example, I have two windows, one for server and one for client. As soon as the client connected to the server, it should appear the time at the server window....

Thanks,

Toan Pham
 
jschell
Posts:36,985
Registered: 11/3/97
Re: MAC Address   
Apr 11, 2005 4:36 PM (reply 9 of 10)  (In reply to #8 )

 
Thanks for your input. Is it possible to display the
client time as soon as the client connected to the
server? For example, I have two windows, one for
server and one for client. As soon as the client
connected to the server, it should appear the time at
the server window....

So you have a client and you want the server (not the client) to display the MAC address of the client?

If ALL of the following are true then you can do this....
1. The client and server must be on a lan (not different lans, not a wan, not the internet.)
2. You must use OS specific code - the Java API does not have anything that will do this.

Now if either of the above is not possible then it suggests one of the following...
1. You misunderstood the assignment (for instance the teacher intends for you to write the client so it sends the MAC address to the server.)
2. Your teacher has no idea what they are talking about.
 
ajmasx
Posts:188
Registered: 10/21/03
Re: MAC Address   
Apr 25, 2006 7:07 AM (reply 10 of 10)  (In reply to original post )

 
I came across a way, but you need to note that
- it requires access to comand line tool (arp)
- it only works on a LAN
- it is not guaranteed to work every time

Also, I haven't included the ProcessOutputHandler class, which just reads an input stream and places the text into a string buffer, until the stream is closed. The ping is important, since without it the likelyhood of arp knowing the address is reduced. I wouldn't consider this 'industrial strength'.

    /**
     * Tested to work with results from ARP command under Solaris, Linux, MS-Windows
     * Note it does not always return a Mac address, even if the host exists. This represents more of 
     * a best 'effort' solution.
     */
    private String getMacAddress ( String host ) throws UnknownHostException, IOException, InterruptedException
    {
        String macAddress = null;
        InetAddress hostAddress = InetAddress.getByName( host );
        
        String resultLine = callArpTool ( hostAddress.getHostAddress() );
        
        if ( resultLine == null )
        {
            return null;
        }
        if ( resultLine.indexOf("\n") > -1 )
        {
            String[] lines = resultLine.split("\n");
            resultLine = lines[lines.length -1];
        }
        if ( resultLine.indexOf("--") > -1 || resultLine.indexOf("unknown host") > -1 )
        {
            return null;
        }
                
        int idx = -1;
        if ( resultLine.indexOf( '-' ) > -1 )
        {
            idx = resultLine.indexOf( '-' ) - 2;
        }
        else if ( resultLine.indexOf( ':' ) > -1 )
        {
            idx = resultLine.indexOf( ':' ) - 2;
        }        
          
        int endIdx = resultLine.length()-1;
        if ( resultLine.indexOf(' ',idx+1) > -1 )
        {
            endIdx = resultLine.indexOf(' ',idx+1);
        }
        macAddress = resultLine.substring(idx,endIdx);
        
        return macAddress;
    }
    
    private String callArpTool ( String ipAddress ) throws IOException, InterruptedException
    {
        String result = null;
        /* samples:
        //solaris
        result = "motako (10.1.12.79) at 0:e:a6:b5:70:80";
        result = "arp: motakox: unknown host";
        //linux
        result = "Address                  HWtype  HWaddress           Flags Mask            Iface\n"+
                 "whyme.xxxxxxxxxxxxxxx.c  ether   00:11:D8:DF:2B:9C   C                     eth2";
        result = "batoo.yyyyyy.org (70.55.60.9) -- no entry";
        //windows
        result = " 56 3:  10.1.12.203           00-11-d8-df-2b-9c     dynamic";
        result = " 2 12:Interface: 10.1.12.134 --- 0x50003";
        */
         
        if ( System.getProperty("os.name").toLowerCase().startsWith("windows") )
        {
            return callArpToolWindows( ipAddress );
        }
 
        return callArpToolDefault( ipAddress );
    }
    
    private String callArpToolWindows ( String ipAddress ) throws IOException, InterruptedException
    {
        String[] cmdArray = null;
        
        cmdArray = new String[] { "ping", ipAddress };
        
        Runtime.getRuntime().exec( cmdArray ).waitFor();
        
        cmdArray = new String[] { "arp", "-a" };
        
        StringBuilder stdOut = new StringBuilder();
        Process proc = Runtime.getRuntime().exec( cmdArray );
        new ProcessOutputHandler(stdOut,proc.getInputStream(),2001);
        
        proc.waitFor();
        
        String[] parts = stdOut.toString().split("\n");
        for ( String part : parts )
        {            
            if ( part.indexOf(ipAddress) > -1 )
            {
                return part;
            }
        }
        return null;
     }
    
    private String callArpToolDefault ( String ipAddress ) throws IOException, InterruptedException
    {
        String[] cmdArray = null;
        
        cmdArray = new String[] { "ping", ipAddress };
        
        Runtime.getRuntime().exec( cmdArray ).waitFor();
        
        cmdArray = new String[] { "arp", ipAddress };
        
        StringBuilder stdOut = new StringBuilder();
        Process proc = Runtime.getRuntime().exec( cmdArray );
        new ProcessOutputHandler(stdOut,proc.getInputStream(),2001);
        
        proc.waitFor();
        
        return stdOut.toString();
     }
 
This topic has 10 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 56
  • Guests : 127

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums