Advertisement
anechka_ne_plach

Untitled

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