3

I am trying to collect a building's mac addresses, and I have created a web application using Vaadin 6(the application has other features so I have to stick with vaadin).

The problem is, I want my user to only insert his name, and for me to get his mac address automatically, but the problem is I don't know how to do it.

I was using

ip = InetAddress.getLocalHost();
mac = ip.getHostAddress();

The problem is that this code returns me my own servers ip and mac.

So the question is, how can I get the client's mac address??

Also if i can get the local ip that is so much better, but first I need the mac.

Any help is very much appreciated.

Mihai
  • 1,212
  • 16
  • 25
  • @user2511414 You *can* get the MAC address by IP address; and, rather than being 'really unlogical', it is a fundamental operation or the ARP protocol of TCP/IP. However it can only be done for IP addresses on the same subnet. The real question here is 'for what purpose'? There's nothing useful you can do with a MAC address in Java except display it somewhere. – user207421 Oct 23 '13 at 22:03
  • @EJP good point, i just didn't the scope of the work and I meant the internet. –  Oct 23 '13 at 22:08
  • Ejp, i need the mac adresss for my firewall, everybody will get a static ip, and with the mac i identify the pc. So mayby you can post the solution on how to get that mac? – Mihai Oct 24 '13 at 04:17

1 Answers1

3

IP address of the client in a servlet you can get so -

HttpServletRequest httpServletRequest = (HttpServletRequest) request;

// Proxy
String userIpAddress = httpServletRequest.getHeader("X-Forwarded-For");

if(userIpAddress == null) {
   userIpAddress = request.getRemoteAddr();
}

In Vaadin there are class WebBrowser, you can use it -

WebBrowser browser = (WebBrowser) getWindow().getTerminal();
String userIpAddress = browser.getAddress();

MAC address of sender changing as many times as times the frame passes through routers and you will always receive the MAC address of the network gateway.

  • Thank you very much for the reply. I didn't really understant the part with the mac and what it does betwen the nodes. What if I have a server on the lan, then teoreticaly the connection would be direct right?, could I get the mac then? – Mihai Oct 23 '13 at 21:06