首页 新闻 会员 周边

在C#中如何使用MAC地址进行通信?

0
悬赏园豆:30 [已解决问题] 解决于 2018-10-03 10:54

在局域网中,若干个通信模块的IP地址是相同的。在已知所有设备的MAC地址的情况下,怎么根据MAC地址向特定的设备通信?不能使用IP地址。谢谢!

smartgoogle的主页 smartgoogle | 初学一级 | 园豆:41
提问于:2018-09-29 11:16
< >
分享
最佳答案
0

需要调用 win32 API,参考 Send directly to MAC address over Ethernet

To communicate at the ethernet/ARP level you need to P/Invoke to the win32 IPHelper APIs. Ie. to send an ARP:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Net.Mail;
using System.IO;

namespace ConsoleApplication3
{
    class Program
    {
        
        [System.Runtime.InteropServices.DllImport("iphlpapi.dll", ExactSpelling=true)]
        public static extern int SendARP(int DestIP, int SrcIP, byte[] pMacAddr, ref int PhyAddrLen);

        static void Main(string[] args)
        {
            IPAddress ipAddress = IPAddress.Parse("x.x.x.x");
            byte[] macBytes = new byte[6];
            int length = 6;
            SendARP(BitConverter.ToInt32(ipAddress.GetAddressBytes(), 0), 0, macBytes, ref length);
            Console.WriteLine( BitConverter.ToString(macBytes, 0, 6));          

            Console.WriteLine("PROGRAM END....");
            Console.ReadKey();
        }
    }
}
收获园豆:30
dudu | 高人七级 |园豆:30994 | 2018-09-29 15:18

多谢!

smartgoogle | 园豆:41 (初学一级) | 2018-10-03 10:53
其他回答(1)
0

IP会相同???

jqw2009 | 园豆:2439 (老鸟四级) | 2018-09-30 15:21

某些通讯模块可以人为设置IP地址

支持(0) 反对(0) smartgoogle | 园豆:41 (初学一级) | 2018-10-03 10:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册