Java get visitor IP and MAC address

Source: Internet
Author: User

Get the visitor's IP, code:

<%string sip= ""; try{sip = (String) request.getremoteaddr ();} catch (Exception e) {}%>

Gets the visitor MAC address, which is used by the UDP protocol to send the query Mac instruction and then return the value to resolve the Mac. Code

<%string SMAC = "";   String sip = Request.getheader ("X-forwarded-for"); if (sip = = NULL | | sip.length () = = 0 | |   "Unknown". Equalsignorecase (SIP)) {sip = Request.getheader ("Proxy-client-ip"); } if (sip = = NULL | | sip.length () = = 0 | |   "Unknown". Equalsignorecase (SIP)) {sip = Request.getheader ("Wl-proxy-client-ip"); } if (sip = = NULL | | sip.length () = = 0 | |   "Unknown". Equalsignorecase (SIP)) {sip = REQUEST.GETREMOTEADDR (); } udpgetclientmacaddr Umac = new udpgetclientmacaddr (SIP); SMAC = Umac. %>...<input value=<%=smac%> name= "usermacaddr" size= "getremotemacaddr" style= "width:150px" readonly= "yes" > Udpgetclientmacaddr.javapackage cch;import java.io.*;import java.net.*;p ublic class    udpgetclientmacaddr {private String sremoteaddr;    private int iremoteport=137;    Private byte[] buffer = new byte[1024];        Private Datagramsocket Ds=null;     Public udpgetclientmacaddr (String straddr) throws exception{sremoteaddr = straddr;   ds = new Datagramsocket (); } Protected final Datagrampacket send (final byte[] bytes) throws IOException {Datagrampacket DP = new Datagram     Packet (Bytes,bytes.length,inetaddress.getbyname (SREMOTEADDR), iremoteport);        Ds.send (DP);    return DP; } protected final Datagrampacket receive () throws Exception {Datagrampacket dp = new Datagrampacket (buffer,buf     Fer.length);     Ds.receive (DP);    return DP;     } protected byte[] Getquerycmd () throws Exception {byte[] T_ns = new BYTE[50];        T_ns[0] = 0x00;        T_NS[1] = 0x00;        T_NS[2] = 0x00;        T_NS[3] = 0x10;        T_NS[4] = 0x00;        T_NS[5] = 0x01;        T_NS[6] = 0x00;        T_NS[7] = 0x00;        T_NS[8] = 0x00;        T_NS[9] = 0x00;        T_NS[10] = 0x00;        T_NS[11] = 0x00;        T_NS[12] = 0x20;        T_NS[13] = 0x43;                T_NS[14] = 0x4B;        for (int i = +; i <; i++) {t_ns[i] = 0x41; } t_ns[45] = 0x00;       T_NS[46] = 0x00;        T_NS[47] = 0x21;        T_NS[48] = 0x00;     T_NS[49] = 0x01;    return T_ns;        } Protected Final String getmacaddr (byte[] brevdata) throws Exception {int i = brevdata[56] * 18 + 56;        String saddr= "";                        StringBuffer sb = new StringBuffer (17);            for (int j = 1; J < 7;j++) {saddr = integer.tohexstring (0xFF & Brevdata[i+j]);            if (Saddr.length () < 2) {sb.append (0);            } sb.append (Saddr.touppercase ());        if (J < 6) sb.append (': ');    } return sb.tostring ();     Public final void Close () {try {ds.close ();     } catch (Exception ex) {ex.printstacktrace ();     }} public final String getremotemacaddr () throws Exception {byte[] bqcmd = Getquerycmd ();     Send (Bqcmd);        Datagrampacket DP = receive ();        String SMAC = getmacaddr (Dp.getdata ());     Close ();           return SMAC; }}

  

Java get visitor IP and MAC address

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.