這裏是winsock的方式。它只是提取嵌入在服務器發送的HTML代碼中的IP地址。
此代碼使用http://api.ipify.org/。
下面是兩個不同的代碼。一個用於VS2012-2015,使用strcpy_s()
,另一個用於使用strcpy()
的Visual C++ 6.0,因爲VS2012-2015會拋出錯誤消息,提示您使用strcpy_s()
而不是strcpy()
。
Visual Studio 2012-2015的代碼。
#include "stdafx.h"
#include <string.h>
#include <winsock2.h>
#include <windows.h>
#include <iostream>
#include <vector>
#include <locale>
#include <sstream>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
string website_HTML;
locale local;
void get_Website(string url);
char lineBuffer[200][80] = { ' ' };
char buffer[10000];
char ip_address[16];
int i = 0, bufLen = 0, j = 0, lineCount = 0;
int lineIndex = 0, posIndex = 0;
//****************************************************
int main(void){
cout << "\n\n\n";
get_Website("api.ipify.org");
for (size_t i = 0; i<website_HTML.length(); ++i) website_HTML[i] = tolower(website_HTML[i], local);
istringstream ss(website_HTML);
string stoken;
while (getline(ss, stoken, '\n')) {
//cout <<"-->"<< stoken.c_str() << '\n';
strcpy_s(lineBuffer[lineIndex], stoken.c_str());
int dot = 0;
for (int ii = 0; ii< strlen(lineBuffer[lineIndex]); ii++){
if (lineBuffer[lineIndex][ii] == '.') dot++;
if (dot >= 3){
dot = 0;
strcpy_s(ip_address, lineBuffer[lineIndex]);
}
}
lineIndex++;
}
cout << "Your IP Address is " << ip_address << " \n\n";
cout << "\nPress ANY key to close.\n\n";
cin.ignore(); cin.get();
return 0;
}
//****************************************************
void get_Website(string url){
WSADATA wsaData;
SOCKET Socket;
SOCKADDR_IN SockAddr;
int lineCount = 0;
int rowCount = 0;
struct hostent *host;
string get_http;
get_http = "GET/HTTP/1.1\r\nHost: " + url + "\r\nConnection: close\r\n\r\n";
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0){
cout << "WSAStartup failed.\n";
system("pause");
//return 1;
}
Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
host = gethostbyname(url.c_str());
SockAddr.sin_port = htons(80);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0){
cout << "Could not connect";
system("pause");
//return 1;
}
send(Socket, get_http.c_str(), strlen(get_http.c_str()), 0);
int nDataLength;
while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0){
int i = 0;
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r'){
website_HTML += buffer[i];
i += 1;
}
}
closesocket(Socket);
WSACleanup();
}
的Visual C++ 6.0代碼
#include <string.h>
#include <winsock2.h>
#include <windows.h>
#include <iostream>
#include <vector>
#include <locale>
#include <sstream>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
string website_HTML;
locale local;
void get_Website(string url);
char lineBuffer[200][80] ={' '};
char buffer[10000];
char ip_address[16];
int i = 0, bufLen=0, j=0,lineCount=0;
int lineIndex=0, posIndex=0;
//****************************************************
int main(void){
cout << "\n\n\n";
get_Website("api.ipify.org");
for (size_t i=0; i<website_HTML.length(); ++i) website_HTML[i]= tolower(website_HTML[i],local);
istringstream ss(website_HTML);
string stoken;
while(getline(ss, stoken, '\n')) {
strcpy(lineBuffer[lineIndex],stoken.c_str());
int dot=0;
for (int ii=0; ii< strlen(lineBuffer[lineIndex]); ii++){
if (lineBuffer[lineIndex][ii] == '.') dot++;
if (dot>=3){
dot=0;
strcpy(ip_address,lineBuffer[lineIndex]);
}
}
lineIndex++;
}
cout<<"Your IP Address is "<< ip_address<<" \n\n";
cout<<"\nPress ANY key to close.\n\n";
cin.ignore(); cin.get();
return 0;
}
//****************************************************
void get_Website(string url){
WSADATA wsaData;
SOCKET Socket;
SOCKADDR_IN SockAddr;
int lineCount=0;
int rowCount=0;
struct hostent *host;
string get_http;
get_http = "GET/HTTP/1.1\r\nHost: " + url + "\r\nConnection: close\r\n\r\n";
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0){
cout << "WSAStartup failed.\n";
system("pause");
//return 1;
}
Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
host = gethostbyname(url.c_str());
SockAddr.sin_port=htons(80);
SockAddr.sin_family=AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0){
cout << "Could not connect";
system("pause");
//return 1;
}
send(Socket,get_http.c_str(), strlen(get_http.c_str()),0);
int nDataLength;
while ((nDataLength = recv(Socket,buffer,10000,0)) > 0){
int i = 0;
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r'){
website_HTML+=buffer[i];
i += 1;
}
}
closesocket(Socket);
WSACleanup();
}
你不知道。我的意思是你沒有使用C++獲取接口的IP地址(* any * interface),因爲C++沒有任何類型的網絡概念。而是使用本機平臺特定的功能來獲取接口列表。如果你搜索一下,你會在互聯網上找到很多關於如何做到這一點的例子,包括很多在這個網站上,嘗試搜索例如*枚舉網絡接口窗口api * –
@JoachimPileborg - 我知道如何枚舉網絡接口。但是,我特別需要使用C++編程方式查找外部IP。我希望有本地API可以幫助我完成這一點。 – spdcbr
你是指你的電腦「外部」地址,還是路由器?你可以很容易地找出一個,但不是其他的。可以使用http://www.whatsmyip.org/之類的東西,也可以直接查找路由器。如果您不想使用第三方http請求庫,請查看[Windows Internet函數](https://msdn.microsoft.com/en-us/library/windows/desktop/aa383630(v = vs.85)的.aspx)。 –