Created
July 17, 2019 12:45
-
-
Save uroni/adca370a15befc66898b5f93f78efed8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #include <iostream> | |
| #include <sys/types.h> | |
| #include <netinet/in.h> | |
| #include <sys/socket.h> | |
| #include <arpa/inet.h> | |
| #include <netdb.h> | |
| #include <stdio.h> | |
| void print_in_addr(struct sockaddr *sa) { | |
| char buf[100]; | |
| if (sa->sa_family == AF_INET) { | |
| inet_ntop(AF_INET, &(((struct sockaddr_in*)sa)->sin_addr), buf, sizeof(buf)); | |
| std::cout << "Address (ipv4): " << buf << std::endl; | |
| return; | |
| } | |
| inet_ntop(AF_INET6, &(((struct sockaddr_in6*)sa)->sin6_addr), buf, sizeof(buf)); | |
| std::cout << "Address (ipv6): " << buf << std::endl; | |
| } | |
| int main(int argc, char* argv[]) | |
| { | |
| if(argc!=2) { | |
| std::cout << "Not enough arguments" << std::endl; | |
| return 1; | |
| } | |
| struct addrinfo hints = {}; | |
| hints.ai_family = AF_UNSPEC; | |
| hints.ai_socktype = SOCK_STREAM; | |
| int rv; | |
| struct addrinfo* servinfo; | |
| if ((rv = getaddrinfo(argv[1], "55415", &hints, &servinfo)) != 0) { | |
| perror("getaddrinfo"); | |
| return 1; | |
| } | |
| for(struct addrinfo* p = servinfo; p != NULL; p = p->ai_next) | |
| { | |
| print_in_addr(p->ai_addr); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment