• C

getting my ip address

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
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi soloboso,

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

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 http://checkip.dyndns.org/. 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: checkip.dyndns.org\r\nConnection: close\r\n\r\n";
    static char *hostname = "checkip.dyndns.org";
    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;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.