是否有可能通过C#中的TCPListener获取远程客户端的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();
}
}
}
}
}
你看IP帮助库https://msdn.microsoft.com/en-us/library/windows/desktop/aa366071 (v = vs.85).aspx – pm100
根据MAC地址是第2层(硬件)地址并且不能在远程设备的局域网外访问的事实,我不认为您想要什么。在本地网络上,您可以执行ARP从IP地址获取MAC地址。 –
MAC用于数据链路层,而不是网络层,所以我非常怀疑它。 – Amy