在局域网中,若干个通信模块的IP地址是相同的。在已知所有设备的MAC地址的情况下,怎么根据MAC地址向特定的设备通信?不能使用IP地址。谢谢!
需要调用 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();
}
}
}
多谢!
IP会相同???
某些通讯模块可以人为设置IP地址