网络编程常用知识

1. 如何获取主机名

2. 如何从域名获取IP地址

3. 如何从struct in_addr和struct hostent结构中获取IP地址

    WORD wVersionRequested;
    WSADATA wsaData;
    int err;
    wVersionRequested = MAKEWORD( 2, 2 );
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 ) {
        /* Tell the user that we could not find a usable */
        /* WinSock DLL.                                  */
        return FALSE;
    }
    /* Confirm that the WinSock DLL supports 2.2.*/
    /* Note that if the DLL supports versions greater    */
    /* than 2.2 in addition to 2.2, it will still return */
    /* 2.2 in wVersion since that is the version we      */
    /* requested.                                        */
    if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 ) {
        /* Tell the user that we could not find a usable */
        /* WinSock DLL.                                  */
        WSACleanup( );
        return FALSE;
    }

    char hostname[255];

    struct hostent *hst = gethostbyname(www.126.com) ;
    struct   in_addr   ia; 

    if(hst   !=   NULL)  
    {  
        memcpy(&ia.s_addr,hst->h_addr_list[0],sizeof(ia.s_addr));  
        int   iIpLength   =   strlen(inet_ntoa(ia))   +   1;  
        char*   lpcIp   =   new   char[iIpLength];  
        ZeroMemory(lpcIp,iIpLength);  
        strcpy(lpcIp,inet_ntoa(ia));  

       delete []lpclp;

  }

    int i = gethostname(hostname, 255);

            WSACleanup( );

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值