-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
62 lines (52 loc) · 1.38 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// main.cpp
// Socket
//
// Created by René Descartes Domingos Muala on 07/02/21.
// Copyright © 2021 landia (René Muala). All rights reserved.
//
// #Sorry for my english!
//
#include <iostream>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#define machine_name_buffer_size 1024
int main(int argc, const char * argv[]) {
/*
1. Our variables!
*/
char machine_name [machine_name_buffer_size] = {0};
char machine_IPv4[INET_ADDRSTRLEN] = {0};
hostent * machine_host = nullptr;
/*
2. Get current machine name
! if the machine is connected to a network:
returns <machine name>
! else
returns <machine name>.local
*/
gethostname(machine_name, 1024);
/*
3. Get the machine host by using the machine name
! if the machine is connected to a network:
returns <Public host>
! else
returns <Local host> (IPv4: 127.0.0.1)
*/
machine_host = gethostbyname(machine_name);
/*
4. Get a readable IPv4 from the machine host
*/
inet_ntop(AF_INET, machine_host->h_addr_list[0], machine_IPv4, INET_ADDRSTRLEN);
/*
5. Print the machine name and the readable IPv4
*/
std::cout
<< "Machine Name: "<<machine_name << std::endl
<< "Machine IPv4: "<<machine_IPv4 << std::endl;
/*
6. Have a Good day!
*/
return 0;
}