C Program to find IP Address, Subnet Mask & Default Gateway

CServer Side ProgrammingObject Oriented Programming

C programming language can be used to find the details of the Internet connection of the system. Now, let’s learn about the basics terms that we need in this problem.

IP Address − IP Address stands for Internet Protocol address. An IP address is a fixed numerical identification number that is associated with each device. IP address allows communication of your device using IP address over the internet.

Subnet Mask − A 32-bit component of the IP address. The subnet mask differentiates the network component of IP address into two parts of the IP address. One being network address and other being a network address. Subnet is the number that is design subs that connect to the networks and complete the IP address of the system connected to the network.

Default Gateway − It is an access point or IP router of a computer that is connected to the network. This default gateway is the gateway that is defined by the computer by default until any other mask gateway is used by an application. This gateway is the connecting route of the network of the systems to the rest of the internet. Failure of this network may disconnect the subnetwork to the internet.

Now, as we have learned all statements related to our work. We now can use code snippets and programs to display these things.

In C programming language there are two methods that can be used in order to check the IP’s of the system.

  • System command
  • Excel command

System command

C programming language provides system() function in stdlib library that can be used to excess the ip configuration of the system using ipcofig. In the call of the function we will pass the full address of the ipconfig file that is needed to be extracted.

Example

#include <stdio.h>
#include <stdlib.h>
int main(){
   system("c:\\windows\\system32\\ipconfig");
   return 0;
}

Excel Command

Another way to fetch the IP details of the system is by using the excel() function. This function needs more than one parameter as in the code.

Example

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
   execl("c:\\windows\\system32\\ipconfig", "ipconfig", 0);
   return 0;
}

The output of these codes depends on the system. IP details of a system are confidential so the outputs are not displayed here but you can run the code and your system to display details.

raja
Published on 19-Sep-2019 09:08:54
Advertisements