Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1 │ #include <sys/types.h>
- 2 │ #include <sys/socket.h>
- 3 │ #include <netdb.h>
- 4 │ #include <stdio.h>
- 5 │ #include <stdlib.h>
- 6 │ #include <netinet/in.h>
- 7 │ #include <arpa/inet.h>
- 8 │
- 9 │ enum { MAX_LEN = 1001 };
- 10 │
- 11 │ char host[MAX_LEN], service[MAX_LEN];
- 12 │
- 13 │ int main() {
- 14 │ struct addrinfo hints = {
- 15 │ .ai_family = AF_INET,
- 16 │ .ai_socktype = SOCK_STREAM
- 17 │ };
- 18 │
- 19 │ while (scanf("%s%s", host, service) != EOF) {
- 20 │ // printf("%s %s\n", host, service);
- 21 │ struct addrinfo* result = NULL;
- 22 │ int err = getaddrinfo(host, service, &hints, &result);
- 23 │ if (err) {
- 24 │ printf("%s\n", gai_strerror(err));
- 25 │ } else {
- 26 │ struct addrinfo* tmp = result;
- 27 │ struct sockaddr_in *ans = (struct sockaddr_in *) tmp->ai_addr;
- 28 │ while (tmp->ai_next) {
- 29 │ tmp = tmp->ai_next;
- 30 │ struct sockaddr_in *ain = (struct sockaddr_in *) tmp->ai_addr;
- 31 │ // printf("test: %s:%d\n", inet_ntoa(ain->sin_addr), ntohs(ain->sin_port));
- 32 │ if (ntohl(ain->sin_addr.s_addr) < ntohl(ans->sin_addr.s_addr)) {
- 33 │ ans = ain;
- 34 │ }
- 35 │ }
- 36 │ printf("%s:%d\n", inet_ntoa(ans->sin_addr), ntohs(ans->sin_port));
- 37 │ }
- 38 │ freeaddrinfo(result);
- 39 │ }
- 40 │
- 41 │ }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement