0

私はどのように取得するのだろうかと思っています:

  • 内部 IP アドレス。
  • 外部 IP アドレス。と
  • デフォルトゲートウェイ

Windows (WinSock) および Unix システムで。

前もって感謝します、

4

3 に答える 3

1

おかげで解決しました: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();
}
于 2009-11-17T22:09:01.597 に答える
1

Windows および UNIX で機能する汎用メカニズムはありません。Windows では、GetIfTable(). ほとんどの UNIX システムでは、 を試してくださいgetifaddrs()。これらは、各インターフェイスの IP アドレスなどのさまざまな情報を提供します。

デフォルトゲートウェイを取得する方法がわかりません。の呼び出しで利用できると思いますsysctlnetstat ユーティリティのソースから始めることをお勧めします。

外部パブリック アドレスは、コンピュータが決して知らないものです。唯一の方法は、インターネット上の何かに接続して、あなたがどのアドレスから来ているかを教えてもらうことです. これは、IPNAT の典型的な問題の 1 つです。

于 2009-11-15T21:04:13.683 に答える
0

Linux:

ifconfig -a gives internal ip
netstat -a   gives default gateway

ウィンドウズ:

ipconfig /all  gives internal ip
netstat -a gives default gateway

どちらのシステムでも外部IPを明確に決定する方法がわかりません

于 2009-11-15T20:54:18.720 に答える