【问题标题】:Get MAC address via TCPListener in C#在 C# 中通过 TCPListener 获取 MAC 地址
【发布时间】:2016-10-28 01:56:58
【问题描述】:

是否可以通过C#中的TCPListener获取远程客户端的MAC地址?

using System;  
using System.Net;  
using System.Net.Sockets;  
using System.IO;  
using System.Text;  

namespace TCPserver  
{  
    class Program  
    {  
        private const int BUFSIZE = 32;  

        static void Main(string[] args)  
        {  
            if (args.Length > 1) // Test for correct of args  
                throw new ArgumentException("Parameters: [<Port>]");  

            int servPort = (args.Length == 1) ? Int32.Parse(args[0]) : 7;  

            TcpListener listener = null;  

            try  
            {  
                // Create a TCPListener to accept client connections  
                listener = new TcpListener(IPAddress.Any, servPort);  
                listener.Start();  
            }  
            catch (SocketException se)  
            {  
                Console.WriteLine(se.Message);  
                Environment.Exit(se.ErrorCode);  
            }  

            byte[] rcvBuffer = new byte[BUFSIZE]; // Receive buffer  
            int bytesRcvd; // Received byte count  

            for (; ; )  
            { // Run forever, accepting and servicing connections  

                TcpClient client = null;  
                NetworkStream ns = null;  
                try  
                {  
                    client = listener.AcceptTcpClient(); // Get client connection  
                    ns = client.GetStream();  
                    Console.Write("Handling client - ");  

                    // Receive until client closes connection  
                    int totalBytesEchoed = 0;  
                    while ((bytesRcvd = ns.Read(rcvBuffer, 0, rcvBuffer.Length)) > 0)  
                    {  
                        ns.Write(rcvBuffer, 0, bytesRcvd);  
                        totalBytesEchoed += bytesRcvd;  
                    }  
                    Console.WriteLine("echoed {0} bytes.", totalBytesEchoed);  

                    ns.Close();  
                    client.Close();  

                }  
                catch (Exception e)  
                {  
                    Console.WriteLine(e.Message);  
                    ns.Close();  
                }  
            }  
        }  
    }  
}  

【问题讨论】:

  • 你有没有看ip helper library msdn.microsoft.com/en-us/library/windows/desktop/…
  • 我不认为您想要什么是可能的,因为 MAC 地址是第 2 层(硬件)地址,并且在远程设备的 LAN 之外无法访问。在本地网络上,您可以执行 ARP 从 IP 地址获取 MAC 地址。
  • MAC用在数据链路层,不是网络层,所以严重怀疑。
  • @E.Moffat 如何转换为 C# 代码?
  • @Amy Well...您介意提供一种通过 IP 地址和端口获取它的正确方法吗?

标签: c# .net sockets mac-address tcplistener


【解决方案1】:

所以答案是否定的,这是不可能的。

但是通过IP地址我们可以得到这样的MAC地址

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication14
{
    class Program
    {
        static void Main(string[] args)
        {
            IPAddress address = IPAddress.Parse("12.3.0.42");
            byte[] t = GetMacAddress(address);
            string mac = string.Join(":", (from z in t select z.ToString("X2")).ToArray());
            Console.WriteLine(mac);
            Console.ReadLine();
        }

        [DllImport("iphlpapi.dll", ExactSpelling = true)]
        public static extern int SendARP(uint destIP, uint srcIP, byte[] macAddress, ref uint macAddressLength);

        public static byte[] GetMacAddress(IPAddress address)
        {
            byte[] mac = new byte[6];
            uint len = (uint)mac.Length;
            byte[] addressBytes = address.GetAddressBytes();
            uint dest = ((uint)addressBytes[3] << 24)
              + ((uint)addressBytes[2] << 16)
              + ((uint)addressBytes[1] << 8)
              + ((uint)addressBytes[0]);
            if (SendARP(dest, 0, mac, ref len) != 0)
            {
                throw new Exception("The ARP request failed.");
            }
            return mac;
        }
    }
}

【讨论】:

  • 为解决问题点赞。其他答案希望您想在不同的网络上执行此操作,但正如您刚刚证明的那样,在 LAN 中这是可能的。感谢您发布答案!
【解决方案2】:

没有。 MAC 地址是Link layer 的一部分,仅用于同一物理链路中的两台主机通信。

过于简单化...,假设ac 是计算机,b 是路由器。

a <-> b <-> c

如果a 想向c 发送数据包,则必须经过b。所以a发送的数据包源IP地址a,目标IP地址c,源MAC地址a和目标MAC地址b,因为路由器是下一跳。然后当b 收到该数据包时,它将使用源IP 地址a、目标IP 地址c、源MAC 地址b 和目标MAC 地址c 将其发送到c

【讨论】:

  • 请帮助获取一些代码以通过 IP 地址获取 MAC 地址然后...
  • 无法通过IP获取MAC地址。您可以获取直接连接的 IP 地址的 MAC 地址,例如使用 `arp ​​-a '。并通过集线器或基本交换机直接连接。
猜你喜欢
  • 2021-12-18
  • 2011-07-07
  • 1970-01-01
  • 2017-10-28
  • 2013-02-27
  • 2016-03-09
  • 2012-08-25
  • 2022-09-29
  • 1970-01-01
相关资源
最近更新 更多