Winsock Programmer's FAQ |
このサンプルでは、あるコンピュータのネットワークインターフェー スのリストを取得する方法を示します。このとき、ブロードキャストア ドレス、ネットマスク、その他インターフェースの能力についてなどの 関連する情報も同時に取得します。このプログラムは、出力のみを行う Unix の ifconfig コマンドのようなもので、実際にちょこっ と役に立つツールです。ですが出力はより読みやすくなっています。 このプログラムは Winsock 2 を必要とします。また Windows NT 4.0 の少なくともサービスパック 3 がインストールされている環境か、 おそらく Windows 2000 でも動作します。Windows 98 でも試してみた ところ、何か動いているみたいですが、多くのフィールドは正しく動作 しませんでした(例えば、インターフェースのネットマスクなど)。また Windows 95 においては、Winsock 2 にアップデートしても動かなかっ た、という報告を受けています。 この仕掛けと問題について記述している MS Knowledge Base の記 事が二つあります: Q181520、Q170642。 getifaces.cpp// Borland C++ 5.0: bcc32.cpp getifaces.cpp ws2_32.lib // Visual C++ 5.0: cl getifaces.cpp ws2_32.lib #include <iostream.h> #include <winsock2.h> #include <ws2tcpip.h> int doit() { SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0); if (sd == SOCKET_ERROR) { cerr << "Failed to get a socket. Error " << WSAGetLastError() << endl; return 1; } INTERFACE_INFO InterfaceList[20]; unsigned long nBytesReturned; if (WSAIoctl(sd, SIO_GET_INTERFACE_LIST, 0, 0, &InterfaceList, sizeof(InterfaceList), &nBytesReturned, 0, 0) == SOCKET_ERROR) { cerr << "Failed calling WSAIoctl: error " << WSAGetLastError() << endl; return 1; } int nNumInterfaces = nBytesReturned / sizeof(INTERFACE_INFO); cout << "There are " << nNumInterfaces << " interfaces:" << endl; for (int i = 0; i < nNumInterfaces; ++i) { cout << endl; sockaddr_in *pAddress; pAddress = (sockaddr_in *) & (InterfaceList[i].iiAddress); cout << " " << inet_ntoa(pAddress->sin_addr); pAddress = (sockaddr_in *) & (InterfaceList[i].iiBroadcastAddress); cout << " has bcast " << inet_ntoa(pAddress->sin_addr); pAddress = (sockaddr_in *) & (InterfaceList[i].iiNetmask); cout << " and netmask " << inet_ntoa(pAddress->sin_addr) << endl; cout << " Iface is "; u_long nFlags = InterfaceList[i].iiFlags; if (nFlags & IFF_UP) cout << "up"; else cout << "down"; if (nFlags & IFF_POINTTOPOINT) cout << ", is point-to-point"; if (nFlags & IFF_LOOPBACK) cout << ", is a loopback iface"; cout << ", and can do: "; if (nFlags & IFF_BROADCAST) cout << "bcast "; if (nFlags & IFF_MULTICAST) cout << "multicast "; cout << endl; } return 0; } int main() { WSADATA WinsockData; if (WSAStartup(MAKEWORD(2, 2), &WinsockData) != 0) { cerr << "Failed to find Winsock 2.2!" << endl; return 2; } int nRetVal = doit(); WSACleanup(); return nRetVal; } |
<< Get the Local IP Address(es) | Get MAC Address, NetBIOS Method >> |
Last modified on 22 September 2001 at 23:48 UTC-7 | Please send corrections to tangent@cyberport.com. |
< Go to the main FAQ page | << Go to the Home Page |