免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6064 | 回复: 2
打印 上一主题 下一主题

紧急求助:怎样用Windows API在Windows下枚举IP地址 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-07-06 03:04 |只看该作者 |倒序浏览
0可用积分
在AIX/Solaris下有ioctl,不知在windows下怎样做\r\n注意用gethostname()然后gethostbyname不行的,因为可能有超过一块网卡,而gethostname+gethostbyname只能得到其中的一个

论坛徽章:
0
2 [报告]
发表于 2007-07-06 03:04 |只看该作者
【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}

论坛徽章:
0
3 [报告]
发表于 2009-10-21 23:12 |只看该作者
WSAIoctl()  简述:控制一个套接口的模式。\r\n  #include <winsock2.h>\r\n  int WSAAPI WSAIoctl(SOCKET s, DWORD\r\n  dwIoControlCode, LPVOID lpvInBuffer, DWORD\r\n  cbInBuffer, LPVOID lpvOutBuffer, DWORD\r\n  cbOutBuffer, LPDWORD lpcbBytesReturned,\r\n  LPWSAOVERLAPPED lpOverlapped,\r\n  LPWSAOVERLAPPED_COMPLETION_ROUTINE\r\n  lpCompletionRoutine);\r\n  s:一个套接口的句柄。\r\n  dwIoControlCode:将进行的操作的控制代码。\r\n  lpvInBuffer:输入缓冲区的地址。\r\n  cbInBuffer:输入缓冲区的大小。\r\n  lpvOutBuffer:输出缓冲区的地址。\r\n  cbOutBuffer:输出缓冲区的大小。\r\n  lpcbBytesReturned:输出实际字节数的地址。\r\n  lpOverlapped:WSAOVERLAPPED结构的地址。\r\n  lpCompletionRoutine:一个指向操作结束后调用的例程指针。\r\n  返回值:\r\n  调用成功后,WSAIoctl ()函数返回0。否则的话,将返回INVALID_SOCKET错误,应用程序可通过WSAGetLastError()来获取相应的错误代码。\r\n  错误代码:\r\n  WSANOTINITIALISED                在调用本API之前应成功调用WSAStartup()。\r\n  WSAENETDOWN                        网络子系统失效。\r\n  WSAEINVAL                                cmd不是一个合法的命令;或者一个输入参数非法;或者命令对于该种类型的套接口不适用。\r\n  WSAEINPROGRESS                在一个回调函数运行时调用了该函数。\r\n  WSAENOTSOCK                        描述字不是一个套接口。\r\n  WSAEOPNOTSUPP                指定的ioctl命令无法实现,例如在SIO_SET_QOS或 SIO_SET_GROUP_QOS中指定的流描述无法实现。\r\n  WSA_IO_PENDING                一个重叠操作被成功启动,过后将报告完成情况。\r\n  WSAEWOULDBLOCK                套接口标志为非阻塞,且所需操作将产生阻塞。\r\n  另请参阅: socket(), ioctlsocket(), WSASocket(),setsockopt(), getsockopt().
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP