Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

how to get ip address of my pc in a c++ program in turbo c++?

plz tell the coding.. or do i need a different compiler

2 Answers

Relevance
  • ?
    Lv 5
    9 years ago
    Favorite Answer

    MS VC++ would be better option or download codeblocks from the link:-

    http://www.codeblocks.org/

    use following headers:-

    #include <windows.h>

    #include <winsock.h>

    using namespave std;

    struct IPv4

    {

    unsigned char b1, b2, b3, b4;

    };

    bool getMyIP(IPv4 & myIP)

    {

    char szBuffer[1024];

    #ifdef WIN32

    WSADATA wsaData;

    WORD wVersionRequested = MAKEWORD(2, 0);

    if(::WSAStartup(wVersionRequested, &wsaData) != 0)

    return false;

    #endif

    if(gethostname(szBuffer, sizeof(szBuffer)) == SOCKET_ERROR)

    {

    #ifdef WIN32

    WSACleanup();

    #endif

    return false;

    }

    struct hostent *host = gethostbyname(szBuffer);

    if(host == NULL)

    {

    #ifdef WIN32

    WSACleanup();

    #endif

    return false;

    }

    //Obtain the computer's IP

    myIP.b1 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b1;

    myIP.b2 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b2;

    myIP.b3 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b3;

    myIP.b4 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b4;

    #ifdef WIN32

    WSACleanup();

    #endif

    return true;

    }

    int main()

    {

    IPv4 ip_add;

    if(getMyIp(&ip_add))

    {

    cout<<"id:"<<ip_add.b1<<........;

    }

    reurn 0;

    }

  • 4 years ago

    you will ought to get a router to get entry to different networks. whether you used a 192.168.15.14 and a 192.168.sixteen.14, you nevertheless could no longer get entry to that gadget because of the fact it extremely is a distinctive community while utilizing type C subnets. in case you used a type B subnet, 255.255.0.0, for the 192.168.15.14 and the 192.168.sixteen.14 you ought to then get entry to the two units because of the fact they may be on the comparable community.

Still have questions? Get your answers by asking now.