在 C# 中通过 TCPListener 获取 MAC 地址
Get MAC address via TCPListener in C#
是否可以通过 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();
}
}
}
}
}
没有。 MAC地址是Link layer的一部分,仅用于在同一物理link.
中的两台主机进行通信
过于简单化...,假设a
和c
是计算机,b
是路由器。
a <-> b <-> c
如果a
要向c
发送数据包,则必须经过b
。因此 a
发送源 IP 地址 a
、目标 IP 地址 c
、源 MAC 地址 a
和目标 MAC 地址 [=13] 的数据包=],因为路由器是下一跳。然后当 b
收到该数据包时,它将使用源 IP 地址 a
、目标 IP 地址 c
、源 MAC 地址 [=13] 将其发送到 c
=] 和目标 MAC 地址 c
.
所以答案是否定的,这是不可能的。
但是通过 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;
}
}
}
是否可以通过 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();
}
}
}
}
}
没有。 MAC地址是Link layer的一部分,仅用于在同一物理link.
中的两台主机进行通信过于简单化...,假设a
和c
是计算机,b
是路由器。
a <-> b <-> c
如果a
要向c
发送数据包,则必须经过b
。因此 a
发送源 IP 地址 a
、目标 IP 地址 c
、源 MAC 地址 a
和目标 MAC 地址 [=13] 的数据包=],因为路由器是下一跳。然后当 b
收到该数据包时,它将使用源 IP 地址 a
、目标 IP 地址 c
、源 MAC 地址 [=13] 将其发送到 c
=] 和目标 MAC 地址 c
.
所以答案是否定的,这是不可能的。
但是通过 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;
}
}
}