[C++] Get Internal IP Address

  • Hello Guest! Welcome to ConsoleCrunch, to help support our site check out the premium upgrades HERE! to get exclusive access to our hidden content.
  • Unable to load portions of the website...
    If you use an ad blocker addon, you should disable it because it interferes with several elements of the site and blocks more than just adverts.
  • Read Rules Before Posting Post Virus Scans with every program.

ZionHD

Leader
Retired Staff
Nov 8, 2014
184
215
53
It only works for Windows OS because it uses windows headers which are not included in linux os.
This code is to get the internal ip address of your pc using C++.

Code:
#pragma once

#include <iostream>
#include <string.h>

#include <WinSock2.h>
#define _WINSOCKAPI_
#include <ws2tcpip.h>

#include <windows.h>
#include <stdio.h>

#pragma comment(lib, "Ws2_32.lib")

using namespace std;

#define DEFAULT_VERSION 1.00

char *get_ip_addr();

int main()
{
    printf("~ IP Displayer %4.2f created by ZionHD ~\n", DEFAULT_VERSION);
    printf("~ Copyright (c) 2015 Console-Forums ~\n");
    printf("\n\n");

    WSAData wsaData;
    if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
        printf("Failed at WSAStartup()\n");
        return 1;
    }

    char *ip = get_ip_addr();

    printf("IP Address: %s", ip);

    WSACleanup();
   
    return 0;
}

char *get_ip_addr()
{
    char ac[256];
    if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) {
        return "127.0.0.1";
    }

    struct hostent *phe = gethostbyname(ac);
    if (phe == 0) {
        return "127.0.0.1";
    }

    struct in_addr addr;
    memcpy(&addr, phe->h_addr_list[0], sizeof(struct in_addr));
    return inet_ntoa(addr);
}
 
  • Like
Reactions: Logan
General chit-chat
Help Users
  • No one is chatting at the moment.
  • @ lurch6661:
    iam good
  • @ lurch6661:
    got my bd disc in
  • @ lurch6661:
    it comes with games lol
  • @ QM|T:
    lol
  • Chat Bot:
    QM|T has joined the room.
  • Chat Bot:
    Tural is our newest member. Welcome!
  • Chat Bot:
    Pharaotix is our newest member. Welcome!
  • Chat Bot:
    Duran_1911 is our newest member. Welcome!
  • Chat Bot:
    LilBoat100 has left the room.
  • Chat Bot:
    keonhacaivoto is our newest member. Welcome!
  • Chat Bot:
    QM|T has joined the room.
  • Chat Bot:
    estro101 is our newest member. Welcome!
  • Chat Bot:
    yeclipsex is our newest member. Welcome!
  • Chat Bot:
    QM|T has joined the room.
  • Chat Bot:
    Diabloron is our newest member. Welcome!
  • Chat Bot:
    mr kiki is our newest member. Welcome!
  • Chat Bot:
    NorwayVon is our newest member. Welcome!
  • Chat Bot:
    QM|T has joined the room.
  • Chat Bot:
    cynthia is our newest member. Welcome!
  • Chat Bot:
    LilBoat100 has joined the room.
  • @ LilBoat100:
    been a min
  • @ LilBoat100:
    i have uncharted 3, (digital) my disc aint working for shit lol, so whenever i try finding "LAN Party" its not on digital, only on disc. so if theres like a eboot maybe, or something i can do to activate the LAN Party to play offline, that shit would be dope bro. anyone.
  • @ LilBoat100:
    or maybe if i download it as iso? idk
      @ LilBoat100: or maybe if i download it as iso? idk