Overview
The Winsock (Windows Sockets) core API provides the fundamental networking primitives for creating TCP/IP and UDP/IP client and server applications on Windows. It follows the Berkeley sockets API with Windows‑specific extensions.
Key Functions
| Function | Description | Header |
|---|---|---|
WSAStartup | Initialises the Winsock library. | winsock2.h |
WSACleanup | Terminates use of the Winsock library. | winsock2.h |
socket | Creates a new socket descriptor. | winsock2.h |
bind | Associates a local address with a socket. | winsock2.h |
listen | Marks a socket as a passive listening socket. | winsock2.h |
accept | Accepts an incoming connection request. | winsock2.h |
connect | Establishes a connection to a remote socket. | winsock2.h |
send | Sends data on a connected socket. | winsock2.h |
recv | Receives data from a connected socket. | winsock2.h |
sendto | Sends a datagram to a specific address. | winsock2.h |
recvfrom | Receives a datagram and captures its source address. | winsock2.h |
getsockopt | Retrieves a socket option value. | winsock2.h |
setsockopt | Sets a socket option value. | winsock2.h |
closesocket | Closes an existing socket. | winsock2.h |
select | Monitors multiple sockets for readiness. | winsock2.h |
Common Structures
Important data structures used with the Winsock API:
sockaddr_in– IPv4 address structure.sockaddr_in6– IPv6 address structure.fd_set– Set of socket descriptors forselect.WSADATA– Holds information about the Windows Sockets implementation.
Typical Initialization Pattern
WSADATA wsaData;
int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result != 0) {
printf("WSAStartup failed: %d\n", result);
return 1;
}
/* ... socket operations ... */
WSACleanup();
Reference Links
Explore detailed documentation for each area: