In the file below I only get output for the first of the two prints. The output that I get is:
0 2 127.0.0.1
0 2
Why does using sockaddr->sa_data (it is a char array) not print anything? While I know it is not a very convenient struct but it should still hold information right? How does the info magically show up after casting it?
Code Used:
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
int main(int argc, char **argv) {
//DNS
struct addrinfo hints;
struct addrinfo *result, *temp;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_CANONNAME;
int s = getaddrinfo("localhost", NULL, &hints, &result);
for (temp = result; temp != NULL; temp = temp->ai_next) {
printf("%d %d %s\n", s, temp->ai_addr->sa_family, inet_ntoa(((struct sockaddr_in*)(temp->ai_addr))->sin_addr));
printf("%d %d %s\n", s, temp->ai_addr->sa_family, temp->ai_addr->sa_data);
}
}