• 得到本地ip


    #ifndef __JRY_SOCKET_H__
    #define __JRY_SOCKET_H__
    typedef enum {
    SUCCESS,
    FAIL,
    I_OPEN_FAIL,
    I_OPENURL_FAIL,
    I_READFILE_FAIL,
    H_QUERYINFO_FAIL,
    }ERRORFLAGS;

    int JRY_WSASTARUP();
    int jry_open_url(char * url, char ** buf, size_t * size);
    char* jry_get_localip(char *);

    #endif

    #include<windows.h>
    #include<Wininet.h>
    #pragma comment(lib,"WinInet.lib")
    #pragma comment(lib,"ws2_32")

    #define JRY_WSACLEANUP() WSACleanup()

    int JRY_WSASTARUP()
    {
    WSAData wsaData={0};
    WORD wVersionRequested=0;
    wVersionRequested = MAKEWORD( 2, 0);
    if(WSAStartup(wVersionRequested, &wsaData))return 0;
    return 1;
    }

    int jry_open_url(char * url, char ** buf, size_t * size)
    {
    HINTERNET hSession=0;
    HINTERNET hRequest=0;
    ERRORFLAGS iret=SUCCESS;
    char qbuf[32]={0};
    unsigned long qsize=sizeof(qbuf);

    do
    {
    char *lpszAgent="InetURL/1.0";
    int dwAccessType=INTERNET_OPEN_TYPE_DIRECT;

    if(!url || !size){iret=FAIL;break;}

    hSession = ::InternetOpen(lpszAgent, dwAccessType, NULL, NULL, 0);
    if(!hSession){iret=I_OPEN_FAIL;break;}

    hRequest=::InternetOpenUrl(hSession, url, NULL, 0, 0, 0);
    if(!hRequest){iret=I_OPENURL_FAIL;break;}

    //if(!::HttpQueryInfo(hRequest, HTTP_QUERY_ALLOW , qbuf, &qsize,0))break;
    if(!::HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH, qbuf, &qsize,0)){iret=H_QUERYINFO_FAIL;break;}

    *size=atol(qbuf);
    *buf=(char*)malloc(atol(qbuf));
    if(!*buf){iret=FAIL;break;}

    if(!InternetReadFile(hRequest, *buf, *size, (unsigned long *)size)){iret=I_READFILE_FAIL;break;}

    } while (0);

    if(hRequest)InternetCloseHandle(hRequest);
    if(hSession)InternetCloseHandle(hSession);
    return iret;
    }

    char* jry_get_localip(char *ip)
    {
    char name[255]={0};
    hostent *phost=NULL;
    do
    {
    if(!JRY_WSASTARUP())break;
    if(gethostname(name,sizeof(name)))break;
    phost=gethostbyname(name);
    if(!phost)break;
    ip=inet_ntoa(*(struct in_addr*)*phost->h_addr_list);
    } while (0);
    JRY_WSACLEANUP();
    return ip;
    }

  • 相关阅读:
    Tomcat集群Cluster实现原理剖析[转] 文件同步
    看到一个比较好的jbpm教程,感谢一下
    vi显示行号
    安装apache2参数详解
    Windows平台下查看占用端口的程序
    struts2中使用token避免重复提交
    在window下安装开源的中文界面的项目管理软件Redmine
    Java中数据存储
    求素数算法网摘
    模式工程化实现及扩展读书笔记——设计原则
  • 原文地址:https://www.cnblogs.com/ccmfc/p/2324912.html
Copyright © 2020-2023  润新知