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.
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?
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.
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 ...
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.
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 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.
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 )
{
returnnull;
}
if ( resultLine.indexOf("\n") > -1 )
{
String[] lines = resultLine.split("\n");
resultLine = lines[lines.length -1];
}
if ( resultLine.indexOf("--") > -1 || resultLine.indexOf("unknown host") > -1 )
{
returnnull;
}
int idx = -1;
if ( resultLine.indexOf( '-' ) > -1 )
{
idx = resultLine.indexOf( '-' ) - 2;
}
elseif ( 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;
}
}
returnnull;
}
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();
}