hostname client, ip client
This commit is contained in:
29
client2/src/main.c
Normal file
29
client2/src/main.c
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
int main() {
|
||||
char *hostname = "www.motionweb.hu";
|
||||
char ip[100];
|
||||
struct hostent *he;
|
||||
struct in_addr **addr_list;
|
||||
int i;
|
||||
|
||||
if ((he = gethostbyname(hostname)) == NULL) {
|
||||
puts("gethostbyname error\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Cast the h_addr_list to in_addr , since h_addr_list also has the ip address in long format only
|
||||
addr_list = (struct in_addr **)he->h_addr_list;
|
||||
|
||||
for (i = 0; addr_list[i] != NULL; i++) {
|
||||
// return the first one
|
||||
strcpy(ip, inet_ntoa(*addr_list[i]));
|
||||
}
|
||||
printf("%s resolved to : %s", hostname, ip);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user