VB icon

Get Internet IP

Email
Submitted on: 8/31/2016 10:24:20 AM
By: Ben128  
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general), Microsoft Visual C++
Views: 1804
 
     This code shows how to get your current internet ip address. this code is made for beginners hope you find it usfull comments welcome.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Get Internet IP
// Description:This code shows how to get your current internet ip address. this code is made for beginners hope you find it usfull comments welcome.
// By: Ben128
//**************************************

// Get IP Address
// Version 1.0
// Date 31/08/2016
// By Ben a.k.a DreamVB
#pragma comment(lib, "urlmon.lib")
#include <iostream>
#include <Windows.h>
using namespace std;
using std::cout;
using std::endl;
int main(int argc, char *argv[]){
	
	string sUrl = "https://api.ipify.org/";
	string sFile = "getip.txt";
	char sLine[80];
	FILE *fp = NULL;
	HRESULT ret;
	char *errmsg = "Cannot fetch ip address.";
	cout << "Getting ip address please wait..." << endl << endl;
	//Get the ip page and save the data to sFile
	ret =	URLDownloadToFileA(NULL, sUrl.c_str(),sFile.c_str(), 0, NULL);
	if (ret != 0){
		cout << errmsg << endl;
		exit;
	}
	//Try and open the filename downloaded.
	fp = fopen(sFile.c_str(), "r");
	if (!fp){
		cout << errmsg << endl;
		exit(1);
	}
	else{
		//Read in the line.
		fgets(sLine, 80, fp);
	}
	//Print out the ip address.
	if (strlen(sLine) > 0){
		cout << "Your ip is: " << sLine << endl;
	}
	//Close file
	fclose(fp);
	//Keep console open.
	system("pause");
	return 0;
}


Other 50 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.