forked from AdarshAddee/Hacktoberfest2022_for_Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98e0d83
commit 6fa79a4
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"configurations": [ | ||
{ | ||
"name": "Win32", | ||
"includePath": [ | ||
"${workspaceFolder}/**" | ||
], | ||
"defines": [ | ||
"_DEBUG", | ||
"UNICODE", | ||
"_UNICODE" | ||
], | ||
"compilerPath": "D:\\MinGW\\bin\\gcc.exe", | ||
"cStandard": "gnu17", | ||
"cppStandard": "gnu++14", | ||
"intelliSenseMode": "windows-gcc-x86" | ||
} | ||
], | ||
"version": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// C program to display hostname | ||
// and IP address | ||
#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> | ||
|
||
// Returns hostname for the local computer | ||
void checkHostName(int hostname) | ||
{ | ||
if (hostname == -1) | ||
{ | ||
perror("gethostname"); | ||
exit(1); | ||
} | ||
} | ||
|
||
// Returns host information corresponding to host name | ||
void checkHostEntry(struct hostent * hostentry) | ||
{ | ||
if (hostentry == NULL) | ||
{ | ||
perror("gethostbyname"); | ||
exit(1); | ||
} | ||
} | ||
|
||
// Converts space-delimited IPv4 addresses | ||
// to dotted-decimal format | ||
void checkIPbuffer(char *IPbuffer) | ||
{ | ||
if (NULL == IPbuffer) | ||
{ | ||
perror("inet_ntoa"); | ||
exit(1); | ||
} | ||
} | ||
|
||
// Driver code | ||
int main() | ||
{ | ||
char hostbuffer[256]; | ||
char *IPbuffer; | ||
struct hostent *host_entry; | ||
int hostname; | ||
|
||
// To retrieve hostname | ||
hostname = gethostname(hostbuffer, sizeof(hostbuffer)); | ||
checkHostName(hostname); | ||
|
||
// To retrieve host information | ||
host_entry = gethostbyname(hostbuffer); | ||
checkHostEntry(host_entry); | ||
|
||
// To convert an Internet network | ||
// address into ASCII string | ||
IPbuffer = inet_ntoa(*((struct in_addr*) | ||
host_entry->h_addr_list[0])); | ||
|
||
printf("Hostname: %s\n", hostbuffer); | ||
printf("Host IP: %s", IPbuffer); | ||
|
||
return 0; | ||
} |