我正在尝试获取当前使用的网络接口的MAC地址。
首先,这个简单的代码基于msdn tcp客户端示例。这是我的客户端应用程序部分的简化版本。它输出用于与服务器建立TCP连接的IP地址(服务器、IP和端口作为命令行参数传递)。在我的客户端应用程序中,我从IP地址获取MAC,通过getsockname函数从套接字获得MAC。
int __cdecl main(int argc, char **argv)
{
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
int iResult;
// Validate the parameters
if (argc != 3) {
printf("usage: %s server-name port\n", argv[0]);
return 1;
}
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo(argv[1], argv[2], &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return 1;
}
// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
static size_t attempt = 0;
std::cout << "attempt: " << attempt++ << std::endl;
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Connect to server.
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}
{
char ip_address_info [128] = "";
SOCKADDR_IN host = {0};
int host_size = sizeof (SOCKADDR_IN);
int res;
res = getsockname(ConnectSocket, (SOCKADDR*) &host, &host_size);
if (res == 0) {
sprintf (ip_address_info, "%s:%d", inet_ntoa (host.sin_addr), htons (host.sin_port));
std::cout << "ip_address_info: " << ip_address_info << std::endl;
}
else {
std::cout << "res == 0 after getsockname" << std::endl;
}
host_size = sizeof (SOCKADDR_IN);
res = getpeername (ConnectSocket, (SOCKADDR*) &host, &host_size);
if (res == 0) {
sprintf (ip_address_info, "%s:%d", inet_ntoa (host.sin_addr), htons (host.sin_port));
std::cout << "ip_address_info: " << ip_address_info << std::endl;
}
else {
std::cout << "res == 0 after getpeername" << std::endl;
}
}
// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
std::cout << "shutdown run" << std::endl;
// cleanup
closesocket(ConnectSocket);
WSACleanup();
return 0;
}现在是问题和问题!当我尝试在局域网中使用此实用工具时,通过服务器端口3128,卡巴斯基防病毒(而且我假设其他防病毒/防病毒)开始检查它的内容(web端口或类似的东西)。我得到了代理连接:我的应用程序-> Kaspersky ->我的服务器。我从getsockname函数获得的IP值为127.0.0.1。我过去经常在我的应用程序中从IP中获取我目前使用的MAC,但当IP为127.0.0.1时,我无法这样做。当我将服务器端口更改为2370时,一切都可以:我得到了好的IP,得到了MAC。但问题是,安装我的程序的用户有时使用代理服务器,端口3128对他们来说很常见,反病毒也是如此。
P.S.:我只在windows上测试过这个(目前也很感兴趣),但我认为类似的东西在其他平台上是可能的。
发布于 2015-12-22 20:09:13
使用目标套接字地址调用GetBestInterfaceEx。
我相信您使用此函数返回的结果索引值与从MIB_IFROW返回的GetIfTable数组匹配。MIB_IFROW结构有一个名为bPhysAddr的成员值,它是MAC地址。
https://stackoverflow.com/questions/34415761
复制相似问题