How to use the ntohs function correctly?

0

Hello, I'm having trouble converting network byte order to host byte order with ntohs() ". I do not understand why the conversion is giving problems, because the function I am using to convert host byte order to network byte order is htons() , which should technically work, since both functions work with unsigned short .

Here's the piece of code where I make use of the ntohs() function:

void get_host_info(struct host_info *info, struct sockaddr_in addr){

    info->ping=100; //ainda estou trabalhando aqui...
    info->port=ntohs(addr.sin_port);
    strncpy(info->addr, inet_ntoa(addr.sin_addr), 16);

    if(getnameinfo((const struct sockaddr*)&addr, sizeof(struct 
                   sockaddr_in), info->host, NI_MAXHOST, NULL, 0, NI_NAMEREQD)!=0)
    {
        strncpy(info->host, "unknown", 8);
    }
}

Print Code:

//...código...

mvwprintw(win, 5+i, 1, "%s - %s:%hu  %0.2f",
          info[i].host, info[i].addr,
          info[i].port, info[i].ping);

//...e mais código...

Now the output:

The correct one would print 9009 (port I'm using on the client and server) and not 50268 and 50269.

    
asked by anonymous 04.10.2018 / 00:59

0 answers