C++编程发送ARP报文

// arpTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include
#include
#include
#include

#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")


int __cdecl main(int argc, char **argv)
{
    DWORD dwRetVal;
    IPAddr DestIp = 0;
    IPAddr SrcIp = 0;       /* default for src ip */
    ULONG *MacAddr=new ULONG[2];       /* for 6-byte hardware addresses */
    ULONG PhysAddrLen = 6;  /* default to length of six bytes */

    char *DestIpString = "192.168.73.254";
    char *SrcIpString = NULL;

    BYTE *bPhysAddr;
    int i;

   

    DestIp = inet_addr(DestIpString);

    memset(MacAddr, 0xff, sizeof (MacAddr));

    printf("Sending ARP request for IP address: %s/n", DestIpString);

    dwRetVal = SendARP(DestIp, SrcIp, MacAddr, &PhysAddrLen);

    if (dwRetVal == NO_ERROR) {
        bPhysAddr = (BYTE *)  MacAddr;
        if (PhysAddrLen) {
            for (i = 0; i < (int) PhysAddrLen; i++) {
                if (i == (PhysAddrLen - 1))
                    printf("%.2X/n", (int) bPhysAddr[i]);
                else
                    printf("%.2X-", (int) bPhysAddr[i]);
            }
        } else
            printf
            ("Warning: SendArp completed successfully, but returned length=0/n");

    }
    else {
        printf("Error: SendArp failed with error: %d", dwRetVal);
         }

    return 0;
}

你可能感兴趣的:(C++编程发送ARP报文)