getting my ip address

Posted on 2006-03-26
Last Modified: 2010-04-15
i need a routine that will in C that will get my internet ip address not lan ip address,print it on screen and then send me an email of the ip address
Question by:soloboso
    LVL 16

    Expert Comment

    Hi soloboso,

    Unfortunately, this code will be system-specific so what system are you running on?

    LVL 11

    Accepted Solution

    There is no easy way to find your external IP from inside lan. You need to rely on some external service that responds with your IP in some way. Here is some code that gets your IP addresse from It compiles and runs on Windows 2k/XP and Linux but should also work on Unix.

    // save as myip.c
    // WINDOWS: cl -o myip.exe myip.c /link ws2_32.lib
    // LINUX:   gcc -o myip myip.c

    #include <stdio.h>

    #ifdef _WIN32
    #include <winsock2.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netinet/tcp.h>

    struct in_addr GetMyIP()
        static char *req = "GET / HTTP/1.0\r\nHost:\r\nConnection: close\r\n\r\n";
        static char *hostname = "";
        char buf[ 2048 ];
        int sock;
        struct sockaddr_in addr = { AF_INET, htons( 80 ), 0 };
        unsigned long nodelay = 1;
        struct hostent *host;
        unsigned long myip = 0;

        InitSockets(); // only needed for Windows OS

        if( host = gethostbyname( hostname ) )
            addr.sin_addr = *(struct in_addr *) host->h_addr;
            if( ( sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) != -1 )
                setsockopt( sock, IPPROTO_TCP, TCP_NODELAY, (char *) &nodelay, 4 );
                if( connect( sock, (struct sockaddr *) &addr, sizeof( addr ) ) != -1 )
                    if( send( sock, req, strlen( req ), 0 ) != -1 )
                        int len = recv( sock, buf, sizeof( buf ) - 1, 0 );
                        if( len > 0 )
                            char *pos, *end, *token = "Current IP Address: ";
                            buf[ len ] = '\0';
                            if( ( pos = strstr( buf, token ) ) && ( end = strstr( pos, "<" ) ) )
                                pos += strlen( token );
                                *end = '\0';
                                myip = inet_addr( pos );
                                printf( "no address in response\n" );
                            printf( "failed to read response from %s\n", hostname );
                        printf( "failed to send request to %s\n", hostname );
                    printf( "cannot connect to %s\n", hostname );
                close( sock );
                printf( "failed to create socket\n" );
            printf( "not connected to the internet or %s is gone\n", hostname );

        if( !myip )
            printf( "error: %s\n", errno );

        return *(struct in_addr *) &myip;

    int main( int argc, int **argv )
        struct in_addr myip = GetMyIP();
        unsigned char *ip = (unsigned char *) &myip;
        printf( "myip: %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3] );


    static WSADATA wsaData = { 0 };
    int InitSockets()
        int error = 0;
        if( !wsaData.wVersion && ( error = WSAStartup( MAKEWORD( 2, 0 ), &wsaData ) ) )
            return error;
        return 0;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now