Skip to content

Instantly share code, notes, and snippets.

@MAGANER
Created July 5, 2021 19:04
Show Gist options
  • Save MAGANER/092b7eddb59f8a3e98228ebf107669e5 to your computer and use it in GitHub Desktop.
Save MAGANER/092b7eddb59f8a3e98228ebf107669e5 to your computer and use it in GitHub Desktop.
function to get local, external ip address of your machine under Windows OS.
/*
* This solution works for Windows as well,
* i don't know what about Unix-like, but i think
* it can include different implementations with
* macros as Win32 e.t.c.
* So we save ipconfig result to file get its 13 line
* with ipv4 data, save it, delete file and then return data.
*/
#ifndef GET_IP_H
#define GET_IP_H
#include<fstream>
#include<string>
#include<iostream>
using namespace std;
namespace
{
string get_ip()
{
system("ipconfig > .ipdata");
ifstream file(".ipdata");
int counter = 0;
string line;
//13 line contains ipv4 data
while (getline(file, line) && counter != 13) { counter++; }
file.close();
system("erase .ipdata");
//after : there are 2 spaces, don't need it
return line.substr(line.find(":") + 2);
}
};
#endif //GET_IP_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment