इस खंड में हम देखेंगे कि स्थानीय सिस्टम के होस्ट नाम और आईपी पते को आसान तरीके से कैसे देखा जाए। हम होस्ट नाम और आईपी खोजने के लिए एक सी प्रोग्राम लिखेंगे।
निम्नलिखित में से कुछ कार्यों का उपयोग किया जाता है। इन कार्यों के अलग-अलग कार्य हैं। आइए कार्यों और उनके कार्यों को देखें।
| कार्य | विवरण |
|---|---|
| gethostname() | यह स्थानीय कंप्यूटर के लिए मानक होस्ट नाम ढूंढता है। |
| gethostbyname() | यह होस्टडेटाबेस से होस्ट नाम के अनुरूप होस्ट जानकारी ढूंढता है |
| iten_ntoa() | यह एक IPv4 इंटरनेट नेटवर्क पते को ASCII स्ट्रिंग में डॉटेड दशमलव प्रारूप में परिवर्तित करता है। |
उदाहरण कोड
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void check_host_name(int hostname) { //This function returns host name for
local computer
if (hostname == -1) {
perror("gethostname");
exit(1);
}
}
void check_host_entry(struct hostent * hostentry) { //find host info from
host name
if (hostentry == NULL) {
perror("gethostbyname");
exit(1);
}
}
void IP_formatter(char *IPbuffer) { //convert IP string to dotted decimal
format
if (NULL == IPbuffer) {
perror("inet_ntoa");
exit(1);
}
}
main() {
char host[256];
char *IP;
struct hostent *host_entry;
int hostname;
hostname = gethostname(host, sizeof(host)); //find the host name
check_host_name(hostname);
host_entry = gethostbyname(host); //find host information
check_host_entry(host_entry);
IP = inet_ntoa(*((struct in_addr*) host_entry->h_addr_list[0]));
//Convert into IP string
printf("Current Host Name: %s\n", host);
printf("Host IP: %s\n", IP);
} आउटपुट (लिनक्स सिस्टम पर परीक्षण किया गया)
Current Host Name: soumyadeep-VirtualBox Host IP: 127.0.1.1