How to get my own IPV4 address

closed account (ozUkoG1T)
Hi,
I am an learner in C++ Network programming anyway i want to find my IPv4 address but not 127.0.0.1. Please help.
Are you wanting your address on the private network your computer is connected to?

That is, are you talking about a home or work/university/school network? (I'm assuming you're connected to a router of somekind.)

Or your public address, as seen be someone across the Internet?

Andy


Last edited on
closed account (ozUkoG1T)
thanks for asking andy. I want to know my public Ipv4 address
Well, it you haven't acquired a fixed IP address, it's not so easy.

See this thread

How to retrieve the IP Address Assigned to the machine by ISP
http://serverfault.com/questions/18967/how-to-retrieve-the-ip-address-assigned-to-the-machine-by-isp

According to this thread, the simplest solution is

You could also write a script with curl/wget that simply gets the page (http://checkip.dyndns.com/) on occasion.

Otherwise you'll need to talk to your router and ask it! (The information is not going to be on your local machine. It just need to know how to send stuff to your router)

Andy

Edit: Just checked my facts: my router know's its external address, and that of the host it's connected to. But the SNMP service is disabled, as I guess it would be for most domestic routers (by default)
Last edited on
closed account (ozUkoG1T)
Yes . Thanks but the problem is that Once it finds you're IP it compare againt an other IP then if the IP is same then it Shows a message or something so you see the problem .
Sorry, I don't follow you.
closed account (ozUkoG1T)
Sorry about that now i understand what you mean thanks sorry for the bothering and also sorry i just did not read ur post properly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
string line;
ifstream IPFile;
int offset; 
char* search0 = "IPv4 Address. . . . . . . . . . . :";      // search pattern
        
system("ipconfig > ip.txt");

IPFile.open ("ip.txt"); 
if(IPFile.is_open())
{
   while(!IPFile.eof())
   {
   getline(IPFile,line);
   if ((offset = line.find(search0, 0)) != string::npos)
   {
//   IPv4 Address. . . . . . . . . . . : 1
//1234567890123456789012345678901234567890     
   line.erase(0,39);
   cout << line << endl;
   IPFile.close();
   }
}
}
return 0;
}
Last edited on
@SamuelAdams

The code you suggest is inappropriate as the OP does not want his local address (which is given by ipconfig). but his public IP address (as seen by people elsewhere in the Internet)

And even if you do want the local address, there's no need to use the evil system, etc. You just call the function getaddrinfo() -- part of the socket API -- directly (rather than getting ipconfig to make the same call for you!)

Andy

PS If you search cplusplus.com for getaddrinfo you will find assorted examples of its use (and abuse).
Last edited on
Sorry I thought he wanted his local IPv4 Address. yea I know it's a hack

If you have a working example of getaddrinfo(), would love to see it, can't seem to find anything online that works.
Last edited on
The example can be found in MSDN:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#undef UNICODE

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>

// link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")

int __cdecl main(int argc, char **argv)
{

    //-----------------------------------------
    // Declare and initialize variables
    WSADATA wsaData;
    int iResult;
    INT iRetval;

    DWORD dwRetval;

    int i = 1;
    
    struct addrinfo *result = NULL;
    struct addrinfo *ptr = NULL;
    struct addrinfo hints;

    struct sockaddr_in  *sockaddr_ipv4;
//    struct sockaddr_in6 *sockaddr_ipv6;
    LPSOCKADDR sockaddr_ip;

    char ipstringbuffer[46];
    DWORD ipbufferlength = 46;

    // Validate the parameters
    if (argc != 3) {
        printf("usage: %s <hostname> <servicename>\n", argv[0]);
        printf("getaddrinfo provides protocol-independent translation\n");
        printf("   from an ANSI host name to an IP address\n");
        printf("%s example usage\n", argv[0]);
        printf("   %s www.contoso.com 0\n", argv[0]);
        return 1;
    }

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }

    //--------------------------------
    // Setup the hints address info structure
    // which is passed to the getaddrinfo() function
    ZeroMemory( &hints, sizeof(hints) );
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    printf("Calling getaddrinfo with following parameters:\n");
    printf("\tnodename = %s\n", argv[1]);
    printf("\tservname (or port) = %s\n\n", argv[2]);
    
//--------------------------------
// Call getaddrinfo(). If the call succeeds,
// the result variable will hold a linked list
// of addrinfo structures containing response
// information
    dwRetval = getaddrinfo(argv[1], argv[2], &hints, &result);
    if ( dwRetval != 0 ) {
        printf("getaddrinfo failed with error: %d\n", dwRetval);
        WSACleanup();
        return 1;
    }

    printf("getaddrinfo returned success\n");
    
    // Retrieve each address and print out the hex bytes
    for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {

        printf("getaddrinfo response %d\n", i++);
        printf("\tFlags: 0x%x\n", ptr->ai_flags);
        printf("\tFamily: ");
        switch (ptr->ai_family) {
            case AF_UNSPEC:
                printf("Unspecified\n");
                break;
            case AF_INET:
                printf("AF_INET (IPv4)\n");
                sockaddr_ipv4 = (struct sockaddr_in *) ptr->ai_addr;
                printf("\tIPv4 address %s\n",
                    inet_ntoa(sockaddr_ipv4->sin_addr) );
                break;
            case AF_INET6:
                printf("AF_INET6 (IPv6)\n");
                // the InetNtop function is available on Windows Vista and later
                // sockaddr_ipv6 = (struct sockaddr_in6 *) ptr->ai_addr;
                // printf("\tIPv6 address %s\n",
                //    InetNtop(AF_INET6, &sockaddr_ipv6->sin6_addr, ipstringbuffer, 46) );
                
                // We use WSAAddressToString since it is supported on Windows XP and later
                sockaddr_ip = (LPSOCKADDR) ptr->ai_addr;
                // The buffer length is changed by each call to WSAAddresstoString
                // So we need to set it for each iteration through the loop for safety
                ipbufferlength = 46;
                iRetval = WSAAddressToString(sockaddr_ip, (DWORD) ptr->ai_addrlen, NULL, 
                    ipstringbuffer, &ipbufferlength );
                if (iRetval)
                    printf("WSAAddressToString failed with %u\n", WSAGetLastError() );
                else    
                    printf("\tIPv6 address %s\n", ipstringbuffer);
                break;
            case AF_NETBIOS:
                printf("AF_NETBIOS (NetBIOS)\n");
                break;
            default:
                printf("Other %ld\n", ptr->ai_family);
                break;
        }
        printf("\tSocket type: ");
        switch (ptr->ai_socktype) {
            case 0:
                printf("Unspecified\n");
                break;
            case SOCK_STREAM:
                printf("SOCK_STREAM (stream)\n");
                break;
            case SOCK_DGRAM:
                printf("SOCK_DGRAM (datagram) \n");
                break;
            case SOCK_RAW:
                printf("SOCK_RAW (raw) \n");
                break;
            case SOCK_RDM:
                printf("SOCK_RDM (reliable message datagram)\n");
                break;
            case SOCK_SEQPACKET:
                printf("SOCK_SEQPACKET (pseudo-stream packet)\n");
                break;
            default:
                printf("Other %ld\n", ptr->ai_socktype);
                break;
        }
        printf("\tProtocol: ");
        switch (ptr->ai_protocol) {
            case 0:
                printf("Unspecified\n");
                break;
            case IPPROTO_TCP:
                printf("IPPROTO_TCP (TCP)\n");
                break;
            case IPPROTO_UDP:
                printf("IPPROTO_UDP (UDP) \n");
                break;
            default:
                printf("Other %ld\n", ptr->ai_protocol);
                break;
        }
        printf("\tLength of this sockaddr: %d\n", ptr->ai_addrlen);
        printf("\tCanonical name: %s\n", ptr->ai_canonname);
    }

    freeaddrinfo(result);
    WSACleanup();

    return 0;
}
If you have a working example of getaddrinfo(), would love to see it, can't seem to find anything online that works.

Well, I think you need to practice your goggling! It's a vital skill for all programmers, along with the use of other online resource like MSDN, The Linux Foundation, Apple Developer Connection, ... and cplusplus.com (as a searchable resource)

When I google for "getaddrinfo example", the first five hits I get are (all with example code)

Getaddrinfo
http://en.wikipedia.org/wiki/Getaddrinfo

creating sockaddr using getaddrinfo
http://www.geekpage.jp/en/programming/winsock/getaddrinfo-1.php

getaddrinfo.c
http://www.logix.cz/michal/devel/various/getaddrinfo.c.xp

getaddrinfo function (Windows)
http://msdn.microsoft.com/en-gb/library/windows/desktop/ms738520%28v=vs.85%29.aspx

GETADDRINFO(3)
http://www.kernel.org/doc/man-pages/online/pages/man3/getaddrinfo.3.html

Andy
Last edited on
Topic archived. No new replies allowed.