Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <string.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <sys/socket.h>
- #include <limits.h>
- #include <netdb.h>
- enum
- {
- MAX_NAME_SIZE = 2000
- };
- int main() {
- char host[MAX_NAME_SIZE], service[MAX_NAME_SIZE];
- while (scanf("%s %s", host, service) != EOF) {
- struct addrinfo *infoptr;
- struct addrinfo hints = { .ai_family = AF_INET, .ai_socktype = SOCK_STREAM };
- int err = getaddrinfo(host, service, &hints, &infoptr);
- if (err) {
- printf("%s\n", gai_strerror(err));
- fflush(stdout);
- } else {
- struct sockaddr_in* min_saddr = NULL;
- unsigned long int min_local_bytes = ULONG_MAX;
- struct addrinfo *p;
- for(p = infoptr; p != NULL; p = p->ai_next) {
- struct sockaddr_in* saddr = (struct sockaddr_in*)p->ai_addr;
- if (min_local_bytes >= ntohl(saddr->sin_addr.s_addr)) {
- min_saddr = saddr;
- min_local_bytes = ntohl(saddr->sin_addr.s_addr);
- }
- }
- printf("%s:%hu\n", inet_ntoa(min_saddr->sin_addr), ntohs(min_saddr->sin_port));
- fflush(stdout);
- freeaddrinfo(infoptr);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement