Winsock2 API Reference
This section provides detailed documentation for the Winsock 2 (Windows Sockets version 2) API, which offers advanced networking capabilities beyond the original Winsock specification.
Overview
Winsock 2 extends the original Winsock API by providing:
- Support for multiple transport protocols (TCP, UDP, etc.).
- Protocol-independent interfaces.
- Improved Quality of Service (QoS) capabilities.
- Support for asynchronous I/O operations.
- Enhanced error handling and reporting.
Key Concepts
Understanding these concepts is crucial for effective use of the Winsock 2 API:
- Sockets: The fundamental endpoint for network communication.
- Address Families: Specifies the protocol family (e.g., AF_INET for IPv4, AF_INET6 for IPv6).
- Socket Types: Defines the communication semantics (e.g., SOCK_STREAM for connection-oriented, SOCK_DGRAM for connectionless).
- Protocols: The actual communication protocol used (e.g., IPPROTO_TCP, IPPROTO_UDP).
- Overlapped I/O: A mechanism for performing I/O operations asynchronously, allowing the application to continue processing while I/O completes.
- Quality of Service (QoS): Mechanisms to prioritize network traffic and guarantee certain levels of performance.
Core Functions
The following are some of the most frequently used Winsock 2 functions:
Socket Creation and Management
socket()
: Creates a socket.bind()
: Assigns a local address and port to a socket.listen()
: Puts a socket into a listening state for incoming connections.accept()
: Accepts an incoming connection on a listening socket.connect()
: Establishes a connection to a remote host.closesocket()
: Closes an existing socket.
Data Transmission
send()
/sendto()
: Sends data over a socket.recv()
/recvfrom()
: Receives data from a socket.WSASend()
/WSASendTo()
: Asynchronous versions for sending data.WSARecv()
/WSARecvFrom()
: Asynchronous versions for receiving data.
Error Handling
WSAGetLastError()
: Retrieves the specific error code for the last Winsock function call.
Structures
Winsock 2 utilizes several important structures:
Structure Name | Description |
---|---|
SOCKADDR |
Generic socket address structure. |
SOCKADDR_IN |
Socket address structure for IPv4. |
SOCKADDR_IN6 |
Socket address structure for IPv6. |
WSABUF |
Buffer structure used for I/O operations. |
WSAOVERLAPPED |
Structure used for overlapped I/O operations. |
Example: Basic TCP Client
Here's a simplified example demonstrating how to create a TCP client socket, connect to a server, and send data.
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib") // Link with ws2_32.lib
int main() {
WSADATA wsaData;
SOCKET clientSocket;
struct sockaddr_in serverAddr;
const char* message = "Hello from Winsock 2 client!";
int bytesSent;
// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed: %d\n", WSAGetLastError());
return 1;
}
// Create a socket
clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientSocket == INVALID_SOCKET) {
printf("Socket creation failed: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Configure server address
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8080); // Example port
InetPton(AF_INET, _T("127.0.0.1"), &serverAddr.sin_addr.s_addr); // Example IP address
// Connect to the server
if (connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
printf("Connect failed: %d\n", WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return 1;
}
printf("Connected to server.\n");
// Send data
bytesSent = send(clientSocket, message, strlen(message), 0);
if (bytesSent == SOCKET_ERROR) {
printf("Send failed: %d\n", WSAGetLastError());
} else {
printf("Sent %d bytes: %s\n", bytesSent, message);
}
// Close the socket
closesocket(clientSocket);
WSACleanup();
return 0;
}
Note: This is a simplified example. Real-world applications should include more robust error checking, buffer management, and potentially asynchronous operations for better performance.