私はどのように取得するのだろうかと思っています:
- 内部 IP アドレス。
- 外部 IP アドレス。と
- デフォルトゲートウェイ
Windows (WinSock) および Unix システムで。
前もって感謝します、
おかげで解決しました:http: //www.codeguru.com/forum/showthread.php? t = 233261
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "ws2_32.lib")
int main(int nArgumentCount, char **ppArguments)
{
WSADATA WSAData;
// Initialize WinSock DLL
if(WSAStartup(MAKEWORD(1, 0), &WSAData))
{
// Error handling
}
// Get local host name
char szHostName[128] = "";
if(gethostname(szHostName, sizeof(szHostName)))
{
// Error handling -> call 'WSAGetLastError()'
}
SOCKADDR_IN socketAddress;
hostent *pHost = 0;
// Try to get the host ent
pHost = gethostbyname(szHostName);
if(!pHost)
{
// Error handling -> call 'WSAGetLastError()'
}
char ppszIPAddresses[10][16]; // maximum of ten IP addresses
for(int iCnt = 0; (pHost->h_addr_list[iCnt]) && (iCnt < 10); ++iCnt)
{
memcpy(&socketAddress.sin_addr, pHost->h_addr_list[iCnt], pHost->h_length);
strcpy(ppszIPAddresses[iCnt], inet_ntoa(socketAddress.sin_addr));
printf("Found interface address: %s\n", ppszIPAddresses[iCnt]);
}
// Cleanup
WSACleanup();
}
Windows および UNIX で機能する汎用メカニズムはありません。Windows では、GetIfTable()
. ほとんどの UNIX システムでは、 を試してくださいgetifaddrs()
。これらは、各インターフェイスの IP アドレスなどのさまざまな情報を提供します。
デフォルトゲートウェイを取得する方法がわかりません。の呼び出しで利用できると思いますsysctl
。netstat ユーティリティのソースから始めることをお勧めします。
外部パブリック アドレスは、コンピュータが決して知らないものです。唯一の方法は、インターネット上の何かに接続して、あなたがどのアドレスから来ているかを教えてもらうことです. これは、IPNAT の典型的な問題の 1 つです。
Linux:
ifconfig -a gives internal ip
netstat -a gives default gateway
ウィンドウズ:
ipconfig /all gives internal ip
netstat -a gives default gateway
どちらのシステムでも外部IPを明確に決定する方法がわかりません