Internet Protocols
The Win32 Networking API provides a comprehensive set of functions, structures, and constants for working with Internet protocols such as TCP, UDP, ICMP, and IPv6. These APIs enable developers to build robust networked applications for Windows.
Key Protocols
- TCP (Transmission Control Protocol) – Reliable, connection‑oriented communication.
- UDP (User Datagram Protocol) – Connection‑less, low‑latency communication.
- ICMP (Internet Control Message Protocol) – Used for diagnostic and error messages.
- IPv6 (Internet Protocol version 6) – The next‑generation IP addressing scheme.
Common Functions
Winsock2.h Functions
// Initialize Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
// Create a TCP socket
SOCKET ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Resolve the server address and port
struct addrinfo *result = NULL,
hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
iResult = getaddrinfo("example.com", "80", &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed: %d\n", iResult);
WSACleanup();
return 1;
}
// Connect to server.
iResult = connect( ConnectSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}