SOCKADDR, SOCKADDR_IN, sockaddr_in, in_addr

在查看有关TCP、UDP聊天程序代码的时候,出现了SOCKADDR, SOCKADDR_IN, sockaddr_in, in_addr等数据结构,有点乱,在此做个整理。

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.

C++
// 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.

struct sockaddr {
  u_short sa_family;
  char sa_data[14];
};
sa_family

Address family.

sa_data[14]

Contains the address in a protocol-dependent format.

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];
};
Header winsock2.h
Windows Embedded CE Windows CE .NET 4.0 and later
Windows Mobile Windows Mobile Version 5.0 and later

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.
struct sockaddr_in{
  short sin_family;
  unsigned short sin_port;
  IN_ADDR sin_addr;
  char sin_zero[8];
};
sin_family

Address family; must be AF_INET.

sin_port

Internet Protocol (IP) port.

sin_addr

IP address in network byte order.

sin_zero

Padding to make structure the same size as SOCKADDR.

The IP address part of this structure is of type in_addr.

Header winsock2.h
Windows Embedded CE Windows CE 1.0 and later
Windows Mobile Windows Mobile Version 5.0 and later

SOCKADDR_IN structure

The SOCKADDR_IN structure specifies a transport address and port for the AF_INET address family.

Syntax

C++
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

Ws2def.h (include Wsk.h)

IN_ADDR structure

The IN_ADDR structure specifies an IPv4 transport address.

Syntax

C++
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

Inaddr.h (include Wsk.h)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值