0

I am developing a web application which acts as a server. In this application I have implimented ServerSocket for two way communication between server application and wi-fi device which acts as client. I am not connecting to my server directly, I am connecting to my router which port forwards to my system and two way communication is successful. All I want now is to find the MAC address of Wi-fi device which is connected.I have done some research and tried to get the MAC address but failed.Can anyone help me on this.Below is the part of my code.

public class ScheduleJob extends ServletApp implements Job{

private int port = 1717;
public static String number;
String ReceivedData = "";

public void execute(JobExecutionContext context)throws JobExecutionException {
    System.out.println("Starting ... ");

    ServerSocket Sersocket = null;
    System.out.println("Starting the socket server at port:" +port);
    boolean listeningSocket = true;
    try {
        Sersocket = new ServerSocket(port);
        System.out.println("Waiting for clients...");
    } catch (IOException e) {
        System.err.println("Could not listen on port: 1717");
    }
    try {

        while (listeningSocket) {
            Socket scokt = Sersocket.accept();
             InetAddress MachineAdd = scokt.getInetAddress();
            System.out.println("Response-----:" +MachineAdd);
            InetAddress ip = InetAddress.getLocalHost();
            System.out.println("current ip : "+ip);
            NetworkInterface network = NetworkInterface.getByInetAddress(ip);

            byte[] mac = network.getHardwareAddress();
            System.out.print("Current MAC address : ");

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mac.length; i++) {
                sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));        
            }
            System.out.println(sb.toString());
Arun
  • 118
  • 1
  • 14

1 Answers1

2

MAC address is piece of information which belongs to layer 2 of OSI model.

In other words it's not preserved whenever you are communicating using L3 protocol (until you are communicating directly via cross cable or via L2 switch).

I would recommend you to send MAC address as a part of your application communication from client to your server, so it will not be lost when your router will do port forwarding.

Some of already asked questions on this topic:

Community
  • 1
  • 1
rkosegi
  • 12,129
  • 5
  • 46
  • 75
  • I have a small doubt. Is there any other things like MAC ID which is unique to every devices and can be read from server side without client sending. – Arun Mar 01 '16 at 06:13
  • In L3 communication you only get client IP and TCP port. – rkosegi Mar 01 '16 at 06:14
  • thank you friend. i will try sending MAC Id as a part of communication from client as you said. – Arun Mar 01 '16 at 06:16
  • @Arun Don't assume that a MAC address will be unique. It can be easily spoofed. – Kayaman Mar 01 '16 at 06:48
  • ok but is there any thing through which i can detect that the information i receive on server is from a particular client when there are multiple client. – Arun Mar 01 '16 at 06:56