vc 擷取外網ip

來源:互聯網
上載者:User
int GetWan(char *ww)   {WSAData wsaDat; short int Port = 80; WSAStartup(MAKEWORD(1,1),&wsaDat); SOCKET sd = socket(AF_INET, SOCK_STREAM, 0);SOCKADDR_IN sockAddr; memset((void*)&sockAddr,0,sizeof(sockAddr)); sockAddr.sin_family=AF_INET;sockAddr.sin_port=htons(Port); sockAddr.sin_addr.s_addr=inet_addr("219.153.15.76");connect(sd,    (LPSOCKADDR)&sockAddr,    sizeof(sockAddr)); char sendstr[]="GET /ip2city.asp HTTP/1.1\r\nAccept: */*\r\nReferer: ******\r\nUser-Agent: Mozilla/4.0\r\nHost: www.ip138.com\r\nConnection: close\r\n\r\n\0"; char* p; int iLen = strlen(sendstr); p = sendstr; while (iLen){    int iRet = send(sd,p,iLen,0);    if (iRet > 0)    {     p += iRet;     iLen -= iRet;     continue;    }    return 0; } char recvstr[1024]; memset(recvstr,0,1024); p = recvstr; char* iIndex_Head ; iLen = 1024; int need_len = -1; while (iLen){    int iRet = recv(sd,p,iLen,0);    if (iRet <= 0)    {     return 0;    }    p += iRet;    iLen -= iRet;    if ((iIndex_Head = strstr(recvstr,"\r\n\r\n"))&&(need_len==-1))    {     char* iIndex = strstr(recvstr,"Content-Length:");     if (!iIndex)     {      continue;     }     char *p1 = iIndex + strlen("Content-Length:");     need_len = iIndex_Head - recvstr + 2 + atol(p1);       }    if (1024 - iLen >=need_len) { break;      } } char *p1 = strstr(iIndex_Head,"["); char *p0 = strstr(p1,"]"); *p0 = '\0';strcpy(ww,p1+1);closesocket(sd); WSACleanup();return atoi(ww);}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.