2

サブネット内のクライアントの Mac アドレスを読み込もうとしています (ただし、testServer アプリケーションを実行している同じマシン上ではありません)。

@ManagedBean
public class ManagedBeanController implements Serializable {

private static final long serialVersionUID = -3244711761400747261L;

private String hostMac;
private InetAddress hostIp;
private String tmpIp;

// snippet taken from a BalusC post , works great. just pointing out that 
// in case of a request from a client running on the same machine of the server, 
// it return 127.0.0.1 (lookpback) instead of the actual localhost IP
public ManagedBeanController() {    
    HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    tmpIp = request.getRemoteAddr();                            
}

public void ReadHostMac() 
{
    try {

        // InetAddress address = InetAddress.getLocalHost(); **COMMENTED**
        InetAddress address = InetAddress.getByName(tmpIp); 
        System.out.println("Current IP address : " + address.getHostAddress() );
        System.out.println("Host NAME : " + address.getHostName() );

        NetworkInterface network = NetworkInterface.getByInetAddress(address);

        if ( network!=null ) 
        {
            byte[] mac = network.getHardwareAddress();

            if ( mac != null ) 
            {
                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("Current MAC address : " + sb.toString());
                setHostMac(sb.toString());

            } 
            else
            {               
                System.out.println("MAC address NULL");
                setHostMac("MAC address NULL");
            }

        }

    } catch (UnknownHostException e) {

        e.printStackTrace();

    } catch (SocketException e){

        e.printStackTrace();

    }
}

// getters and setters

}

この Bean の戻り値は次
のとおりです。コードで address = InetAddress.getLocalHost(); が使用されている場合。
どのクライアントが接続しているかに関係なく、ローカルホストの Mac アドレスのみを返します。
InetAddress アドレス = InetAddress.getByName(tmpIp);
この場合、ネットワーク プロパティはローカル コールの場合に適切に返されますが、MAC プロパティは返されません。さらに悪いことに、「リモート」(まだサブネット内にある) コールの場合は、ネットワーク プロパティでさえ NULL として返されます。
ヒントをいただければ幸いです、ありがとう

4

0 に答える 0