Get the IP address and MAC address in the same network segment!

Source: Internet
Author: User
1 [dllimport ("iphlpapi. dll")]
2 Private Static extern int sendarp (int32 DEST, int32 host, ref int64 Mac, ref int32 length );
3 [dllimport ("ws2_32.dll")]
4 Private Static exint32 inet_addr (string IP );
5
6 protected void page_load (Object sender, eventargs E)
7 {
8 // place user code here to initialize the page
9 try
10 {
11 string userip = request. userhostaddress;
12 string strclientip = request. userhostaddress. tostring (). Trim ();
13 int32 ldest = inet_addr (strclientip); // ip address of the destination
14 int32 lhost = inet_addr (""); // ip address of the local server
15 int64 macinfo = new int64 ();
16 int32 Len = 6;
17 int res = sendarp (ldest, 0, ref macinfo, ref Len );
18 string mac_src = macinfo. tostring ("X ");
19 if (mac_src = "0 ")
20 {
21 if (userip = "127.0.0.1 ")
22 response. Write ("accessing localhost! ");
23 else
24 response. Write ("Welcome to friends from" + userip +! "+" <Br> ");
25 return;
26}
27
28 while (mac_src.length <12)
29 {
30 mac_src = mac_src.insert (0, "0 ");
31}
32
33 string mac_dest = "";
34
35 For (INT I = 0; I <11; I ++)
36 {
37 If (0 = (I % 2 ))
38 {
39 if (I = 10)
40 {
41 mac_dest = mac_dest.insert (0, mac_src.substring (I, 2 ));
42}
43 else
44 {
45 mac_dest = "-" + mac_dest.insert (0, mac_src.substring (I, 2 ));
46}
47}
48}
49
50 response. Write ("welcome friends from" + userip + "<br>" + ", MAC address" + mac_dest +! "
51
52 + "<br> ");
53}
54 catch (exception ERR)
55 {
56 response. Write (ERR. Message );
57}

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.