- 论坛徽章:
- 0
|
【Winsock编程】用WSAIoCtl列举本机所有ip地址示例 \r\n******************************************************************************\\\r\n* INTRFC.CPP\r\n*\r\n* This program demonstrates how to programmatically enumerate IP interface\r\n* information such as a system\'s IP Address, Subnet mask, and broadcast\r\n* address through the WSAIoctl() API using the SIO_GET_INTERFACE_LIST \r\n* option. Additionally, this sample demonstrates how to interpret IP\r\n* status flags from each IP interface. The flags are defined in the\r\n* Windows Sockets 2 Protocol-Specific Annex specification which can be\r\n* found in the January 98 MSDN Platform SDK.\r\n*\r\n*\r\n* Copyright 1996 - 1998 Microsoft Corporation.\r\n* All rights reserved.\r\n* This source code is only intended as a supplement to\r\n* Microsoft Development Tools and/or WinHelp documentation.\r\n* See these sources for detailed information regarding the\r\n* Microsoft samples programs.\r\n\\******************************************************************************/\r\n#define _WIN32_WINNT 0x0400\r\n#define WINVER 0x0400 \r\n#include <windows.h>\r\n#include <ws2tcpip.h>\r\n#include <stdio.h>\r\n\r\nvoid main(void)\r\n{\r\n WORD versionRequested;\r\n int wsError;\r\n WSADATA winsockData; \r\n SOCKET s;\r\n DWORD bytesReturned;\r\n char* pAddrString;\r\n SOCKADDR_IN* pAddrInet;\r\n u_long SetFlags;\r\n INTERFACE_INFO localAddr[10]; // Assume there will be no more than 10 IP interfaces \r\n int numLocalAddr; \r\n\r\n versionRequested = MAKEWORD(2, 2);\r\n\r\n wsError = WSAStartup(versionRequested, &;winsockData); \r\n if (wsError)\r\n { \r\n fprintf (stderr, \"Startup failed\\n\");\r\n return;\r\n }\r\n\r\n if((s = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0)) == INVALID_SOCKET)\r\n {\r\n fprintf (stderr, \"Socket creation failed\\n\");\r\n WSACleanup();\r\n return;\r\n }\r\n\r\n // Enumerate all IP interfaces\r\n fprintf(stderr, \"Scanning Interfaces . . .\\n\\n\");\r\n wsError = WSAIoctl(s, SIO_GET_INTERFACE_LIST, NULL, 0, &;localAddr,\r\n sizeof(localAddr), &;bytesReturned, NULL, NULL);\r\n if (wsError == SOCKET_ERROR)\r\n {\r\n fprintf(stderr, \"WSAIoctl fails with error %d\\n\", GetLastError());\r\n closesocket(s);\r\n WSACleanup();\r\n return;\r\n }\r\n\r\n closesocket(s);\r\n\r\n // Display interface information\r\n numLocalAddr = (bytesReturned/sizeof(INTERFACE_INFO));\r\n for (int i=0; i<numLocalAddr; i++) \r\n {\r\n pAddrInet = (SOCKADDR_IN*)&;localAddr[i].iiAddress;\r\n pAddrString = inet_ntoa(pAddrInet->sin_addr);\r\n if (pAddrString)\r\n printf(\"IP: %s \", pAddrString);\r\n\r\n pAddrInet = (SOCKADDR_IN*)&;localAddr[i].iiNetmask;\r\n pAddrString = inet_ntoa(pAddrInet->sin_addr);\r\n if (pAddrString)\r\n printf(\" SubnetMask: %s \", pAddrString);\r\n\r\n pAddrInet = (SOCKADDR_IN*)&;localAddr[i].iiBroadcastAddress;\r\n pAddrString = inet_ntoa(pAddrInet->sin_addr);\r\n if (pAddrString)\r\n printf(\" Bcast Addr: %s\\n\", pAddrString);\r\n\r\n SetFlags = localAddr[i].iiFlags;\r\n if (SetFlags &; IFF_UP)\r\n printf(\"This interface is up\");\r\n if (SetFlags &; IFF_BROADCAST)\r\n printf(\", broadcasts are supported\");\r\n if (SetFlags &; IFF_MULTICAST)\r\n printf(\", and so are multicasts\");\r\n if (SetFlags &; IFF_LOOPBACK)\r\n printf(\". BTW, this is the loopback interface\");\r\n if (SetFlags &; IFF_POINTTOPOINT)\r\n printf(\". BTW, this is a point-to-point link\");\r\n printf(\"\\n\\n\");\r\n }\r\n\r\n WSACleanup();\r\n} |
|