Winsock Data Structures
Winsock (Windows Sockets API) defines several key data structures that are used to represent network information, addresses, and socket options. Understanding these structures is crucial for developing network applications with Winsock.
Commonly Used Structures
SOCKADDR
and SOCKADDR_IN
The generic SOCKADDR
structure is used by various network protocols. For TCP/IP, the more specific SOCKADDR_IN
structure is used. This structure contains information about the Internet address family, IP address, and port number.
struct sockaddr {
u_short sa_family; // Address family
char sa_data[14]; // Protocol-specific address information
};
struct sockaddr_in {
short int sin_family; // AF_INET (Address Family: Internet)
unsigned short int sin_port; // Port number
struct in_addr sin_addr; // IP address
char sin_zero[8]; // Not used
};
struct in_addr {
union {
struct {
u_char s_b1, s_b2, s_b3, s_b4;
} S_un_b;
struct {
u_short s_w1, s_w2;
} S_un_w;
u_long S_addr;
} S_un;
};
sin_family
:
short int
Specifies the address family, which must be AF_INET for TCP/IP.
sin_port
:
unsigned short int
Specifies the port number. Must be in network byte order.
sin_addr
:
struct in_addr
Contains the IP address of the host. Must be in network byte order.
WSADATA
The WSADATA
structure is used by the WSAStartup
function to return information about the Windows Sockets implementation.
typedef struct WSAData {
WORD wVersion; // Requested version of Winsock
WORD wHighVersion; // Highest version of Winsock supported
char szDescription[WSADESCRIPTION_LEN+1]; // Description of Winsock
char szSystemStatus[WSASYS_STATUS_LEN+1]; // Status of Winsock
unsigned short iMaxSockets; // Max sockets supported
unsigned short iMaxUdpDg; // Max datagrams supported
char FAR * lpVendorInfo; // Vendor-specific info
} WSADATA;
wVersion
:
WORD
The version of the Winsock DLL that the calling application expects to use.
wHighVersion
:
WORD
The highest version of Winsock supported by the Winsock DLL.
szDescription
:
char[]
A null-terminated string that describes the Winsock implementation.
TIMEVAL
The TIMEVAL
structure is used to specify a timeout value for socket operations, particularly for functions like select
.
struct timeval {
long tv_sec; // Seconds
long tv_usec; // Microseconds
};
tv_sec
:
long
Number of seconds.
tv_usec
:
long
Number of microseconds.
FD_SET
The FD_SET
structure is used with the select
function to represent a set of socket file descriptors that you want to monitor for readiness.
typedef struct fd_set {
u_int fd_count; // how many sockets are in the set
SOCKET fd_array[FD_SETSIZE]; // the sockets
} fd_set;
fd_count
:
u_int
The number of sockets currently in the set.
fd_array
:
SOCKET[]
An array of socket descriptors.
Other Important Structures
LINGER
: Controls socket behavior when closing.HOSTENT
: Represents host information (e.g., from DNS lookups).PROTOENT
: Represents protocol information.SERVENT
: Represents service information.
For detailed information on each structure and its members, please refer to the individual structure documentation.