Program for when a structure statement occurs at different locations

Asked

Viewed 22 times

0

Follow the source code...

#define _WIN32_WINNT 0x0501

#include <stdio.h>
#include <ws2tcpip.h>
#include <winsock.h>
#include <windows.h>

WSADATA data;

struct addrinfo tips, *information;
void exaple_one(void);
void exaple_two(void);

void main(void) {

    printf("chamando o exemplo dois\n");
    exaple_two();
    printf("chamando o exemplo um = ERROR\n");
    exaple_one();
}

void exaple_one(void) {

    struct addrinfo tips, *information;

    WORD wRequestedVersion = MAKEWORD(1, 1);
    int iResult = WSAStartup(wRequestedVersion, &data);
    char localaddr[255];
    if(iResult == SOCKET_ERROR) {
        printf("Error...!");
    }

    getaddrinfo("www.netflix.com", NULL, &tips, &information);
    struct addrinfo *ponteiro;
    ponteiro = information;
    getnameinfo(ponteiro->ai_addr, ponteiro->ai_addrlen, localaddr, sizeof(localaddr), NULL, 0, NI_NUMERICHOST);
    printf("exaple one address = %s\n", localaddr);
}

void exaple_two(void) {

    WORD wRequestedVersion = MAKEWORD(1, 1);
    int iResult = WSAStartup(wRequestedVersion, &data);
    char localaddr[255];
    if(iResult == SOCKET_ERROR) {
        printf("Error...!");
    }

    getaddrinfo("www.netflix.com", NULL, &tips, &information);
    struct addrinfo *ponteiro;
    ponteiro = information;
    getnameinfo(ponteiro->ai_addr, ponteiro->ai_addrlen, localaddr, sizeof(localaddr), NULL, 0, NI_NUMERICHOST);
    printf("example two address = %s\n", localaddr);
}

if you compile and run the above program you will see that the example one function for running the program someone could explain to me why this occurs ?

  • 1

    What’s the problem? Program runtime error? Or compilation yet?

1 answer

0

The error is happening because the structure struct addrinfo tips needs to be initialized before being used by the function getaddrinfo(), and the members ai_addrlen, ai_canonname, ai_addr and ai_next of this structure (addrinfo), obligatorily shall be initialized with 0 or NULL.

Below follows the function code exaple_one corrected:

void exaple_one(void) {

    struct addrinfo tips, *information;

    WORD wRequestedVersion = MAKEWORD(1, 1);
    int iResult = WSAStartup(wRequestedVersion, &data);
    char localaddr[255];
    if (iResult == SOCKET_ERROR) {
        printf("Error...!");
    }
    // *** Importante ***: "Zera" todos os componentes da estrutura
    ZeroMemory(&tips, sizeof(struct addrinfo)); 

    // "dicas" sobre o tipo de socket (não necessário)
    tips.ai_family = AF_UNSPEC;
    tips.ai_socktype = SOCK_STREAM;
    tips.ai_protocol = IPPROTO_TCP;

    getaddrinfo("www.netflix.com", NULL, &tips, &information);
    struct addrinfo *ponteiro;
    ponteiro = information;
    getnameinfo(ponteiro->ai_addr, ponteiro->ai_addrlen, localaddr, sizeof(localaddr), NULL, 0, NI_NUMERICHOST);
    printf("exaple one address = %s\n", localaddr);
}

More details in the documentation (in English): getaddrinfo Function

Browser other questions tagged

You are not signed in. Login or sign up in order to post.