ipconfig command from
Windows PowerShell looks like this:
In this example, you can find that the IPv4 address is listed under
Ethernet adapter Ethernet0. Your system may have more network adapters, and each will have its own IP address. We can tell that this computer is on a local network because the IP address,
192.168.182.133, is in the private IP address range.
On Unix-based systems, we use either the
ip addr commands. The
ifconfig command is the old way and is now deprecated on some systems. The
ip addr command is the new way, but not all systems support it yet.
The IPv4 address is listed next to
inet. In this case, we can see that it's
192.168.182.128. Again, we see that this computer is on a local network because of the IP address range. The same adapter has an IPv6 address listed next to
The following screenshot shows using the
ip addr command on Ubuntu Linux:
The preceding screenshot shows the local IPv4 address as
192.168.182.145. We can also see that the link-local IPv6 address is
ip addr, and
ipconfig, show the IP address or addresses for each adapter on your computer. You may have several. If you are on a local network, the IP addresses you see will be your local private network IP addresses.
If you are behind a NAT, there is often no good way to know your public IP address. Usually, the only resort is to contact an internet server that provides an API that informs you of your IP address.
A few free and public APIs for this are as follows:
You can test out these APIs in a web browser:
Each of these listed web pages should return your public IP address and not much else. These sites are useful for when you need to determine your public IP address from behind an NAT programmatically. We look at writing a small HTTP client capable of downloading these web pages and others in Chapter 6, Building a Simple Web Client.
Now that we've seen the built-in utilities for determining our local IP addresses, let's next look at how to accomplish this from C.