IP Addresse



  • Hallo ich bins schon wieder.

    Ich wähle mich per DSL Router in Internet ein.
    Wie bekomme ich die vom provider zugewiesene
    dynamische IP Adresse heraus?

    Ich danke schon mal allen die mir
    irgendwie weiterhelfen können.



  • Den Code habe ich hier schonmal gepostet, die IP, die du meinst, bekommst du mit CGetIP::GetInternetIP:

    Klasse CGetIP, Datei: CGetIP.h

    #ifndef _CGETIP_H_
    #define _CGETIP_H_
    
    #include <wininet.h>
    
    class CGetIP
    {
    public:
        CGetIP(void);
        ~CGetIP(void);
        bool GetFirstLocalIP(TCHAR*);
        bool GetNextLocalIP(TCHAR*);
        bool GetInternetIP(TCHAR*);
    
    private:
        bool ConnectToFile(void);
        void RecieveFile(TCHAR*);
        void CloseConnection(void);
        void RecievedFileToIP(TCHAR*, TCHAR*);
    
        TCHAR       *m_pszFileName;
        HINTERNET   m_hINet;
        HINTERNET   m_hConnection;
        HINTERNET   m_hData;
        WSADATA     m_wsaData;
        int         m_iLocalIPPos;
    
        struct hostent *m_hostent;
    };
    
    #endif[/code]
    Datei: CGetIP.cpp:
    [cpp]
    #define MAX_LENGTH          1024        
    
    #include <windows.h>
    #include "CGetIP.h"
    
    CGetIP::CGetIP(void)
    {
        WSAStartup(MAKEWORD(1, 1), &m_wsaData);
        m_pszFileName = "checkip.dyndns.org";
    
        m_hINet         = 0;
        m_hConnection   = 0;
        m_hData         = 0;
    }
    
    CGetIP::~CGetIP(void)
    {
        CloseConnection();
        WSACleanup();
    }
    
    bool CGetIP::GetFirstLocalIP(TCHAR *pszIP)
    {
        TCHAR   szHostName[MAX_PATH];
    
        if(gethostname(szHostName, MAX_PATH) == SOCKET_ERROR)
            return false;
        m_hostent = gethostbyname(szHostName);
        if(!m_hostent)
            return false;
    
        m_iLocalIPPos = 0;
    
        return GetNextLocalIP(pszIP);
    }
    
    bool CGetIP::GetNextLocalIP(TCHAR *pszIP)
    {
        if(!m_hostent->h_addr_list[m_iLocalIPPos])
            return false;
    
        lstrcpy(pszIP, inet_ntoa(*(in_addr*)m_hostent->h_addr_list[m_iLocalIPPos]));
    
        m_iLocalIPPos++;
    
        return true;
    }
    
    bool CGetIP::GetInternetIP(TCHAR *pszIP)
    {
        if(!ConnectToFile())
        {
            pszIP[0] = 0;
            return 0;
        }
    
        TCHAR *pszBuffer = (TCHAR*)calloc(MAX_LENGTH, sizeof(TCHAR));
        RecieveFile(pszBuffer);
        CloseConnection();
        RecievedFileToIP(pszBuffer, pszIP);
        free(pszBuffer);
    
        return true;
    }
    
    bool CGetIP::ConnectToFile(void)
    {
        m_hINet = InternetOpen("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG,
                                NULL, NULL, 0);
        if(!m_hINet)
            return 0;
    
        m_hConnection = InternetConnect(m_hINet, m_pszFileName, 80, 0, 0,
                        INTERNET_SERVICE_HTTP, NULL, 0);
        if(!m_hConnection)
        {
            CloseConnection();  
            return 0;
        }
    
        m_hData = HttpOpenRequest(m_hConnection, "GET", 0, 0, 
                        0, 0, INTERNET_FLAG_KEEP_CONNECTION, 0);
        if(!m_hData)
        {
            CloseConnection();
            return 0;
        }
    
        HttpSendRequest(m_hData, 0, 0, 0, 0);
    
        return true;
    }
    
    void CGetIP::RecieveFile(TCHAR *pszBuffer)
    {
        DWORD dwRead;
        TCHAR szBuffer[256];
    
        while(InternetReadFile(m_hData, szBuffer, 255, &dwRead))
        {
            if(!dwRead)
                return;
    
            szBuffer[dwRead] = 0;
            lstrcat(pszBuffer, szBuffer);
        }   
    }
    
    void CGetIP::RecievedFileToIP(TCHAR *pszBuffer, TCHAR *pszIP)
    {
        TCHAR szAddress[] = "Address: ";
        TCHAR *p;
    
        p = strstr(pszBuffer, szAddress);
        if(!p)
            return;
        p += lstrlen(szAddress) * sizeof(TCHAR);
        *strchr(p, '\n') = 0;
        lstrcpy(pszIP, p);
    }
    
    void CGetIP::CloseConnection(void)
    {
        if(m_hINet)
            InternetCloseHandle(m_hINet);
        if(m_hConnection)
            InternetCloseHandle(m_hConnection);
        if(m_hData)
            InternetCloseHandle(m_hData);
    
        m_hINet = 0;
        m_hConnection = 0;
        m_hData = 0;
    }
    

    MfG

    P.S.: Das könnte man doch mal ins FAQ stellen.


Log in to reply