Netzwerk - Computer Pingen



  • Kann mir jemand sagen, wie ich in C einen Computer pingen kann. Aber natürlich nicht
    mit
    system ("Ping 192.168.0.33") ;
    Danke



  • Hm, da geistert eine Lösung durch's Netz, die sich auf der leider veralteten ICMP.dll abstützt... ist eine Klasse für C++, aber sollte kein Problem in C sein.

    Header

    #ifndef CPING_H
    #define CPING_H
    
    typedef struct tagIPINFO
    {
        u_char Ttl;                // Time To Live
        u_char Tos;                // Type Of Service
        u_char IPFlags;            // IP flags
        u_char OptSize;            // Size of options data
        u_char FAR *Options;    // Options data buffer
    }IPINFO;
    
    typedef IPINFO* PIPINFO;
    
    typedef struct tagICMPECHO
    {
        u_long Source;            // Source address
        u_long Status;            // IP status
        u_long RTTime;            // Round trip time in milliseconds
        u_short DataSize;        // Reply data size
        u_short Reserved;        // Unknown
        void FAR *pData;        // Reply data buffer
        IPINFO    ipInfo;            // Reply options
    }ICMPECHO;
    
    typedef ICMPECHO* PICMPECHO;
    
    class CPing
    {
    public:
        CPing();
        ~CPing();
        BOOL Ping(char* strHost);
    private:
        // ICMP.DLL Export Function Pointers
        HANDLE (WINAPI *pIcmpCreateFile)(VOID);
        BOOL (WINAPI *pIcmpCloseHandle)(HANDLE);
        DWORD (WINAPI *pIcmpSendEcho)
            (HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD);
        HANDLE hndlIcmp;            // LoadLibrary() handle to ICMP.DLL
        BOOL bValid; // if it doesn't construct properly, it won't be valid
    };
    
    #endif
    
    und dann die Implementation:
    
    Code:
    
    #include "CPing.h"
    
    CPing::CPing()
    {
        bValid = FALSE;
        WSADATA wsaData;            // WSADATA
        int nRet;                    // General use return code
    
        // Dynamically load the ICMP.DLL
        hndlIcmp = LoadLibrary("ICMP.DLL");
        if (hndlIcmp == NULL)
        {
            ::MessageBox(NULL, "Could not load ICMP.DLL", "Error:", MB_OK);
            return;
        }
        // Retrieve ICMP function pointers
        pIcmpCreateFile  = (HANDLE (WINAPI *)(void))
            GetProcAddress((HMODULE)hndlIcmp,"IcmpCreateFile");
        pIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
            GetProcAddress((HMODULE)hndlIcmp,"IcmpCloseHandle");
        pIcmpSendEcho = (DWORD (WINAPI *)
            (HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD))
            GetProcAddress((HMODULE)hndlIcmp,"IcmpSendEcho");
        // Check all the function pointers
        if (pIcmpCreateFile == NULL        | | 
            pIcmpCloseHandle == NULL    | |
            pIcmpSendEcho == NULL)
        {
            ::MessageBox(NULL, "Error loading ICMP.DLL", "Error:", MB_OK);
            FreeLibrary((HMODULE)hndlIcmp);
            return;
        }
    
        // Init WinSock
        nRet = WSAStartup(0x0101, &wsaData );
        if (nRet)
        {
            ::MessageBox(NULL, "WSAStartup() error:", "Error:", MB_OK);
            WSACleanup();
            FreeLibrary((HMODULE)hndlIcmp);
            return;
        }
        // Check WinSock version
        if (0x0101 != wsaData.wVersion)
        {
            ::MessageBox(NULL, "No WinSock version 1.1 support found", "Error:", MB_OK);
            WSACleanup();
            FreeLibrary((HMODULE)hndlIcmp);
            return;
        }
        bValid = TRUE;
    }
    
    CPing::~CPing()
    {
        WSACleanup();
        FreeLibrary((HMODULE)hndlIcmp);
    }
    
    int CPing::Ping(char* strHost)
    {
        struct in_addr iaDest;        // Internet address structure
        LPHOSTENT pHost;            // Pointer to host entry structure
        DWORD *dwAddress;            // IP Address
        IPINFO ipInfo;                // IP Options structure
        ICMPECHO icmpEcho;            // ICMP Echo reply buffer
        HANDLE hndlFile;            // Handle for IcmpCreateFile()
    
        if(!bValid)
        {
            return FALSE;
        }
    
        // Lookup destination
        // Use inet_addr() to determine if we're dealing with a name
        // or an address
        iaDest.s_addr = inet_addr(strHost);
        if (iaDest.s_addr == INADDR_NONE)
            pHost = gethostbyname(strHost);
        else
            pHost = gethostbyaddr((const char *)&iaDest, 
                            sizeof(struct in_addr), AF_INET);
        if (pHost == NULL)
        {
            return FALSE;
        }
    
        // Copy the IP address
        dwAddress = (DWORD *)(*pHost->h_addr_list);
    
        // Get an ICMP echo request handle        
        hndlFile = pIcmpCreateFile();
    
        // Set some reasonable default values
        ipInfo.Ttl = 255;
        ipInfo.Tos = 0;
        ipInfo.IPFlags = 0;
        ipInfo.OptSize = 0;
        ipInfo.Options = NULL;
        icmpEcho.Status = 0;
        // Reqest an ICMP echo
        pIcmpSendEcho(
            hndlFile,        // Handle from IcmpCreateFile()
            *dwAddress,        // Destination IP address
            NULL,            // Pointer to buffer to send
            0,                // Size of buffer in bytes
            &ipInfo,        // Request options
            &icmpEcho,        // Reply buffer
            sizeof(struct tagICMPECHO),
            1000);            // Time to wait in milliseconds
        // Print the results
        iaDest.s_addr = icmpEcho.Source;
        if (icmpEcho.Status)
        {
            return FALSE;
        }
    
        // Close the echo request file handle
        pIcmpCloseHandle(hndlFile);
        return TRUE;
    }
    

    Aufruf:

    CPing Ping;
    int nTime;
    nTime = Ping.Ping("www.yahoo.com");
    nTime = Ping.Ping("192.168.1.1");
    

    liefert dann die Zeit in Millisekunden.

    Für modernere Lösungen via Winsock2 mußt Du mal bei www.codeguru.com suchen.

    [ 09.07.2001: Beitrag editiert von: class CMarcus ]


Anmelden zum Antworten