Winsock Structures
This section provides detailed information about the data structures used by the Windows Sockets API (Winsock). Understanding these structures is crucial for developing network applications using Windows Sockets.
WSAData
Structure
The WSAData
structure contains information about the Winsock implementation. This structure is populated by the WSAStartup
function.
Structure Definition:
typedef struct WSAData {
WORD wVersion;
WORD wHighVersion;
char FAR * lpVendorInfo;
char FAR * szDescription[WSADESCRIPTION_LEN + 1];
char FAR * szSystemStatus[WSASYSTEMSTATUS_LEN + 1];
unsigned short iMaxSockets;
unsigned short iServices;
char FAR * lpVendorInfo;
} WSAData;
Members:
wVersion
: The version of the Winsock protocol specification that the DLL is capable of supporting.
wHighVersion
: The highest version of the Winsock protocol specification that the DLL can support.
lpVendorInfo
: A far pointer to a string that contains additional information about the Winsock provider.
szDescription
: A NULL-terminated string describing the Winsock implementation.
szSystemStatus
: A NULL-terminated string containing further status information about the Winsock implementation.
iMaxSockets
: The maximum number of Windows Sockets that can be opened simultaneously.
iServices
: A bitmask that describes the services supported by the Winsock provider.
SOCKADDR_IN
Structure
The SOCKADDR_IN
structure is used to specify an endpoint for a network connection using the Internet Protocol (IP) version 4. It is a part of the more general SOCKADDR
structure.
Structure Definition:
typedef struct sockaddr_in {
short int sin_family;
unsigned short int sin_port;
struct in_addr sin_addr;
char sin_zero[8];
} sockaddr_in;
Members:
sin_family
: Address family. Must be AF_INET
for IP version 4.
sin_port
: Port number.
sin_addr
: An in_addr
structure that contains the IP address.
sin_zero
: Padding to make the structure the same size as the generic SOCKADDR
structure.
SOCKADDR_IN6
Structure
The SOCKADDR_IN6
structure is used to specify an endpoint for a network connection using the Internet Protocol (IP) version 6. It is part of the AF_INET6
address family.
Structure Definition:
typedef struct sockaddr_in6 {
ADDRESS_FAMILY sin6_family;
USHORT sin6_port;
ULONG sin6_flowinfo;
struct in6_addr sin6_addr;
union {
ULONG64 sin6_align;
struct {
UCHAR s6_addr8[16];
} sin6_addr8;
};
} sockaddr_in6;
Members:
sin6_family
: Address family. Must be AF_INET6
for IP version 6.
sin6_port
: Port number.
sin6_flowinfo
: Flow information for the socket.
sin6_addr
: An in6_addr
structure that contains the IP address.
ADDRINFO
Structure
The ADDRINFO
structure is used with the getaddrinfo
function to retrieve address information for a specified socket address.
Structure Definition:
typedef struct addrinfo {
int ai_flags;
int ai_family;
int ai_socktype;
int ai_protocol;
size_t ai_addrlen;
struct sockaddr *ai_addr;
char * ai_canonname;
struct addrinfo *ai_next;
} ADDRINFO;
Members:
ai_flags
: Flags that affect the behavior of the getaddrinfo
function.
ai_family
: The protocol family for the socket (e.g., AF_INET
, AF_INET6
).
ai_socktype
: The socket type (e.g., SOCK_STREAM
, SOCK_DGRAM
).
ai_protocol
: The protocol to be used (e.g., IPPROTO_TCP
, IPPROTO_UDP
).
ai_addrlen
: The length, in bytes, of the buffer pointed to by the ai_addr
member.
ai_addr
: A pointer to a SOCKADDR
structure that contains the socket address.
ai_canonname
: A pointer to a NULL-terminated string that contains the canonical name for the host.
ai_next
: A pointer to the next structure in a linked list of ADDRINFO
structures.
HOSTENT
Structure
The HOSTENT
structure is used by Winsock functions to return host information, including the host name, aliases, and IP addresses.
Structure Definition:
struct hostent {
char FAR * h_name; // Official name of the host.
char FAR * FAR * h_aliases; // Array of aliases for the host.
short h_addrtype; // Type of the host address (AF_INET or AF_INET6).
short h_length; // Length of the address in bytes.
char FAR * FAR * h_addr_list; // Array of network addresses for the host.
};
Members:
h_name
: A pointer to the NULL-terminated string that contains the official name of the host.
h_aliases
: A pointer to an array of NULL-terminated strings that are the network aliases for the host.
h_addrtype
: The type of the host address. This will be AF_INET
or AF_INET6
.
h_length
: The length, in bytes, of the h_addr_list
.
h_addr_list
: A pointer to an array of pointers, where each pointer is to a buffer containing a network address for the host. The array is terminated by a NULL pointer.
LINGER
Structure
The LINGER
structure controls the behavior of a socket when data is still queued to be sent or received.
Structure Definition:
typedef struct linger {
u_short l_onoff; // Enable or disable the linger option.
u_short l_linger; // Time out value in seconds.
} LINGER;
Members:
l_onoff
: If non-zero, the SO_LINGER
option is enabled for the socket. If zero, the option is disabled.
l_linger
: A time-out value, in seconds, for the SO_LINGER
option. This value is only relevant if l_onoff
is non-zero.
ADDRESS_INFO
Structure
The ADDRESS_INFO
structure is a general structure for socket addresses, typically used with functions like bind
, connect
, and sendto
.
Structure Definition:
typedef struct sockaddr {
unsigned short sa_family; // Address family (e.g., AF_INET, AF_INET6)
char sa_data[14]; // Socket address data
} SOCKADDR, *PSOCKADDR, SOCKADDR_storage, *PSOCKADDR_storage;
Members:
sa_family
: The address family to which the socket address belongs.
sa_data
: Contains the socket address information, which varies depending on the address family.
SOCKADDR
Structure
The SOCKADDR
structure is a generic structure for socket addresses. It's often used as a base for more specific address structures like SOCKADDR_IN
and SOCKADDR_IN6
.
Structure Definition:
typedef struct sockaddr {
unsigned short sa_family; // Address family (e.g., AF_INET, AF_INET6)
char sa_data[14]; // Socket address data
} SOCKADDR, *PSOCKADDR;
Members:
sa_family
: The address family to which the socket address belongs.
sa_data
: Contains the socket address information, which varies depending on the address family.
SOCKADDR_UN
Structure
The SOCKADDR_UN
structure is used for UNIX domain sockets, which are used for interprocess communication on a single host. Winsock supports this for compatibility.
Structure Definition:
typedef struct sockaddr_un {
short int sun_family; // AF_UNIX
char sun_path[108]; // Pathname
} sockaddr_un;
Members:
sun_family
: Address family. Must be AF_UNIX
.
sun_path
: The pathname of the socket.
SERVENT
Structure
The SERVENT
structure is used by Winsock functions to return information about a specific service, such as its name, protocol, and port number.
Structure Definition:
struct servent {
char FAR * s_name; // Official name of the service.
char FAR * FAR * s_aliases; // Array of aliases for the service.
int s_proto; // Protocol for the service.
int s_port; // Port number for the service.
};
Members:
s_name
: A pointer to the NULL-terminated string that contains the official name of the service.
s_aliases
: A pointer to an array of NULL-terminated strings that are the aliases for the service.
s_proto
: The protocol to be used for the service (e.g., IPPROTO_TCP, IPPROTO_UDP).
s_port
: The port number for the service.
IP_MREQR
Structure
The IP_MREQR
structure is used to specify an IP multicast group and the local interface to use for joining or leaving the group.
Structure Definition:
typedef struct ip_mreq {
struct in_addr imr_multiaddr; // IP multicast group address.
struct in_addr imr_interface; // Local interface address.
} IP_MREQR;
Members:
imr_multiaddr
: An in_addr
structure that contains the IP address of the multicast group.
imr_interface
: An in_addr
structure that contains the IP address of the local interface to use for joining or leaving the group.
This is a partial list of Winsock structures. Refer to the official Microsoft documentation for a comprehensive overview.