我的程序大体如下:
HOSTENT *tmpHostent ;
unsigned long  ip2 = *( unsigned long * )( "192.168.3.201" ) ;
in_addr tmp ; 
tmp =  *( in_addr * )&ip2 ;
ip2 = inet_addr( inet_ntoa( tmp ) ) ;
tmpHostent = gethostbyaddr( (char*)&ip2 , 16 , AF_INET ) ;
CString ss=tmpHostent->h_name;
AfxMessageBox(ss);为什么执行后,内存报错?

解决方案 »

  1.   

    get hode of long long-distance machine's mac address#include "..."#pragma comment ( lib, "ws2_32.lib" )void main( int argc, char ** argv )
    {
        int numberOfHost = 1;
        struct hostent *remoteHostent;    //处理命令行参数
        if ( argc == 3 )
        numberOfHost = atoi( argv[2] );
        if ( ( argc >3 ) || ( argc < 2 ) )
        {
            printf( "RmtHost v0.2 - Get remote HostName /MacAddress\n" );
            printf( "by ShotgunLabs ( [email protected] )\n\n" );
            printf( "Usage :\n\tRmtHost.exe [RemoteIP] \n\n" );
            printf( "Example:\n\tRmtHost.exe 192.168.0.3\n" );
            printf( "\tRmtHost.exe 192.168.0.3 255\n\n" );
            exit( 0 );
        }     //初始化SOCKET
        WSADATA wsaData;
        int iRet = WSAStartup(MAKEWORD(2,1), &wsaData);
        if ( iRet != 0 )
        {
            printf( "WSAStartup Error:%d\n", GetLastError() );
            exit( 0 );
        }
        int nRemoteAddr = inet_addr( argv[1] );
        remoteHostent= (struct hostent*)malloc( sizeof(struct hostent ));
        struct in_addr sa;
        for ( int i = 0; i < numberOfHost; i ++ )
        {
            //获取远程机器名
            sa.s_addr = nRemoteAddr;
            printf( "\nIpAddress : %s\n", inet_ntoa( sa ) );
            remoteHostent = gethostbyaddr( (char*)&nRemoteAddr,4, AF_INET );
            if ( remoteHostent )
                printf( "HostName : %s\n",remoteHostent->h_name );
            else
                printf( "gethostbyaddr Error:%d\n",GetLastError() );
            //发送ARP查询包获得远程MAC地址        unsigned char macAddress[6];
            ULONG macAddLen = 6;
            iRet=SendARP(nRemoteAddr, (unsigned long)NULL,(PULONG)&macAddress, &macAddLen);
            if ( iRet == NO_ERROR )
            {
                printf( "MacAddress: " );
                for( int i =0; i<6; i++ )
                {
                    printf( "%.2x", macAddress[i] );
                    if ( i<5 ) printf( "-" );
                }
                printf( "\n" );
            }
            else
                printf( "SendARP Error:%d\n", GetLastError());
                nRemoteAddr = htonl( ntohl( nRemoteAddr ) + 1 );
        }
    }
      

  2.   

    HOSTENT *tmpHostent ;
    unsigned long  ip2 = *( unsigned long * )( "192.168.3.201" ) ;
    //why? i think is this 
    //unsigned long ip2 = inet_addr( "192.168.3.201" );
    in_addr tmp ; 
    tmp =  *( in_addr * )&ip2 ;
    ip2 = inet_addr( inet_ntoa( tmp ) ) ;
    tmpHostent = gethostbyaddr( (char*)&ip2 , 16 , AF_INET ) ;
      

  3.   

    呵呵,好像是从我哪里copy过去的.
    HOSTENT *tmpHostent ;
    unsigned long  ip2 = *( unsigned long * )( "192.168.3.201" ) ;
    in_addr tmp ; 
    tmp =  *( in_addr * )&ip2 ;
    ip2 = inet_addr( inet_ntoa( tmp ) ) ;
    tmpHostent = gethostbyaddr( (char*)&ip2 , 16 , AF_INET ) ;你这个应该判断一下 tmpHosten == NULL  的情况
    CString ss=tmpHostent->h_name;
    AfxMessageBox(ss);