sockaddr
The sockaddr structure varies depending on the protocol selected. Except for the sin*_family parameter, sockaddr contents are expressed in network byte order.
Winsock functions using sockaddr are not strictly interpreted to be pointers to a sockaddr structure. The structure is interpreted differently in the context of different address families. The only requirements are that the first u_short is the address family and the total size of the memory buffer in bytes is namelen.
The SOCKADDR_STORAGE structure also stores socket address information and the structure is sufficiently large to store IPv4 or IPv6 address information. The use of the SOCKADDR_STORAGE structure promotes protocol-family and protocol-version independence, and simplifies development. It is recommended that the SOCKADDR_STORAGEstructure be used in place of the sockaddr structure. The SOCKADDR_STORAGE structure is supported on Windows Server 2003 and later.
The sockaddr structure and sockaddr_in structures below are used with IPv4. Other protocols use similar structures.
struct sockaddr { ushort sa_family; char sa_data[14]; }; struct sockaddr_in { short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; };
The sockaddr_in6 and sockaddr_in6_old structures below are used with IPv6.
struct sockaddr_in6 { short sin6_family; u_short sin6_port; u_long sin6_flowinfo; struct in6_addr sin6_addr; u_long sin6_scope_id; }; typedef struct sockaddr_in6 SOCKADDR_IN6; typedef struct sockaddr_in6 *PSOCKADDR_IN6; typedef struct sockaddr_in6 FAR *LPSOCKADDR_IN6; struct sockaddr_in6_old { short sin6_family; u_short sin6_port; u_long sin6_flowinfo; struct in6_addr sin6_addr; };
On the Microsoft Windows Software Development Kit (SDK) released for Windows Vista and later, SOCKADDR andSOCKADDR_IN typedef tags are defined for sockaddr and sockaddr_in structures as follows:
typedef struct sockaddr { #if (_WIN32_WINNT < 0x0600) u_short sa_family; #else ADDRESS_FAMILY sa_family; #endif //(_WIN32_WINNT < 0x0600) CHAR sa_data[14]; } SOCKADDR, *PSOCKADDR, FAR *LPSOCKADDR; typedef struct sockaddr_in { #if(_WIN32_WINNT < 0x0600) short sin_family; #else //(_WIN32_WINNT < 0x0600) ADDRESS_FAMILY sin_family; #endif //(_WIN32_WINNT < 0x0600) USHORT sin_port; IN_ADDR sin_addr; CHAR sin_zero[8]; } SOCKADDR_IN, *PSOCKADDR_IN;
On the Windows SDK released for Windows Vista and later, the organization of header files has changed and the sockaddr and sockaddr_in structures are defined in the Ws2def.h header file, not the Winsock2.h header file. TheWs2def.h header file is automatically included by the Winsock2.h header file. The sockaddr_in6 structure is defined in theWs2ipdef.h header file, not the Ws2tcpip.h header file. The Ws2ipdef.h header file is automatically included by theWs2tcpip.h header file. The Ws2def.h and Ws2ipdef.h header files should never be used directly.
Example Code
The following example demonstrates the use of the sockaddr structure.
// Declare variables SOCKET ListenSocket; struct sockaddr_in saServer; hostent* localHost; char* localIP; // Create a listening socket ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Get the local host information localHost = gethostbyname(""); localIP = inet_ntoa (*(struct in_addr *)*localHost->h_addr_list); // Set up the sockaddr structure saServer.sin_family = AF_INET; saServer.sin_addr.s_addr = inet_addr(localIP); saServer.sin_port = htons(5150); // Bind the listening socket using the // information in the sockaddr structure bind( ListenSocket,(SOCKADDR*) &saServer, sizeof(saServer) );
sockaddr
This structure varies depending on the protocol selected. Except for the sa_family member, sockaddr contents are expressed in network byte order.
For Winsock functions that use sockaddr, the name parameter is not strictly interpreted as a pointer to a sockaddr structure. It is presented in this manner for Windows Sockets compatibility. The actual structure is interpreted differently in the context of different address families. The only requirements are that the first u_short
is the address family and the total size of the memory buffer in bytes is namelen.
The following structure declaration is used with TCP/IP. Other protocols use similar structures.
struct sockaddr_in {
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
sockaddr_in
In the Internet address family, this structure is used by Windows Sockets to specify a local or remote endpoint address to which to connect a socket. This is the form of the sockaddr structure specific to the Internet address family and can be cast to sockaddr.SOCKADDR_IN structure
The SOCKADDR_IN structure specifies a transport address and port for the AF_INET address family.
Syntax
typedef struct sockaddr_in { ADDRESS_FAMILY sin_family; USHORT sin_port; IN_ADDR sin_addr; CHAR sin_zero[8]; } SOCKADDR_IN, *PSOCKADDR_IN;
Members
-
sin_family
-
The address family for the transport address. This member should always be set to AF_INET.
sin_port
-
A transport protocol port number.
sin_addr
-
An IN_ADDR structure that contains an IPv4 transport address.
sin_zero
-
Reserved for system use. A WSK application should set the contents of this array to zero.
Remarks
All of the data in the SOCKADDR_IN structure, except for the address family, must be specified in network-byte-order (big-endian).
Requirements
Version | Available in Windows Vista and later versions of the Windows operating systems. |
---|---|
Header |
|
IN_ADDR structure
The IN_ADDR structure specifies an IPv4 transport address.
Syntax
typedef struct in_addr { union { struct { UCHAR s_b1; UCHAR s_b2; UCHAR s_b3; UCHAR s_b4; } S_un_b; struct { USHORT s_w1; USHORT s_w2; } S_un_w; ULONG S_addr; } S_un; } IN_ADDR, *PIN_ADDR, *LPIN_ADDR;
Members
-
S_un
-
A union that contains the following different representations of the IPv4 transport address:
-
S_un_b
-
A structure that contains four UCHAR-typed values.
-
s_b1
-
A UCHAR value.
s_b2
-
A UCHAR value.
s_b3
-
A UCHAR value.
s_b4
-
A UCHAR value.
S_un_w
-
-
A structure that contains two USHORT-typed values.
-
s_w1
-
A USHORT value.
s_w2
-
A USHORT value.
S_addr
-
-
A ULONG value.
-
Remarks
A WSK application typically uses only the S_un.S_addr member of the structure. A WSK application can conveniently access this structure member by using the S_addr macro that is defined as follows:
#define s_addr S_un.S_addr
All members of the IN_ADDR structure must be specified in network-byte-order (big-endian).
Requirements
Version | Available in Windows Vista and later versions of the Windows operating systems. |
---|---|
Header |
|