1. Hello Guest! Welcome to the Official ConsoleCrunch website. To help support our site, please disable Adblock and purchase a Premium Upgrade HERE! to get exclusive access to our hidden content.

    We hope that you enjoy your time with us! Thank you.

  2. Please DO NOT repost Console ID's, check if they are posted first!
    Dismiss Notice
  3. Want to Advertise Here? Contact @Younis
  4. Visit our dev section, where you can get help with your code, view examples, and ask for help from those with experience. We are currently after more developers, so now's your time to shine!
    Dismiss Notice
  5. Read Rules Before Posting Post Virus Scans with every program.
    Dismiss Notice

[C++] Get Internal IP Address

Discussion in 'Programming' started by ZionHD, Apr 19, 2015.

  1. ZionHD

    ZionHD Leader Retired Staff

    Joined:
    Nov 8, 2014
    Messages:
    184
    Likes Received:
    215
    Trophy Points:
    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);
    }
    
     
    Logan likes this.
  2. Logan

    Logan Active Poster Retired Staff Active Member

    Joined:
    Nov 9, 2013
    Messages:
    717
    Likes Received:
    467
    Trophy Points:
    133
    will probs never use this but left a like bcuz your cool :cool:
     
Loading...

Share This Page

Top