Click here to Skip to main content
15,888,121 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys
I use windows function getaddrinfo to get the IPv4 address of a host.
C++
struct addrinfo * aiList = NULL;
struct addrinfo aiHints;
//initialize aiHints here
aiHints.ai_family = AF_INET;
int iResult = getaddrinfo( szHostName, NULL, &aiHints, &aiList );
const struct addrinfo * aiPtr = aiList;
while ( aiPtr != NULL )
{
    const struct sockaddr * pSockAddr = aiPtr->ai_addr;
//  print contents of pSockAddr code here
    aiPtr = aiPtr->ai_next;
}

declaration for struct sockaddr is:
C++
struct sockaddr {
 ushort sa_family;
 char sa_data[14];
};
The function succeed and the values of aiPtr->ai_addr->sa_data for localhost is as follows
{ 0, 0, 127, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
Why two leading zeros here?
May it be ai_addr is of type struct sockaddr_in and The two leading zeros is member sin_port of sockaddr_in?
C++
struct sockaddr_in{
   short sin_family;
   unsigned short sin_port;
   struct in_addr sin_addr;
   char sin_zero[8];
}
I'm confused. can any body clarify whats happening?
Thanks in advance

Edit: the original subject was this:
Question about struct addrinfo returned by getaddrino
Posted
Updated 30-Sep-13 7:33am
v3

1 solution

When you use AF_INET the ai_addr member points to an object of type sockaddr_in. Take a look at the example here [^].
 
Share this answer
 
Comments
mr.abzadeh 1-Oct-13 12:45pm    
Thanks. Your right
Only in the sample code ai_addr has been casted to (sockaddr_in *);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900