Overview
The Win32 Networking API provides a comprehensive set of functions, structures, and constants for building network-enabled applications on Windows. It includes support for sockets, raw IP, DNS resolution, multicast, and secure communications.
Key Functions
socket– Create a new socket.bind– Bind a socket to a local address.connect– Connect to a remote host.listen– Prepare a socket to accept connections.accept– Accept an incoming connection.recv/send– Receive and send data.getaddrinfo– Resolve hostnames to address structures.WSAStartup/WSACleanup– Initialize and terminate Winsock.
Important Structures
sockaddr_in– IPv4 socket address.sockaddr_in6– IPv6 socket address.addrinfo– Result ofgetaddrinfo.WSADATA– Winsock version information.
Simple TCP Client
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
int main() {
WSADATA wsaData;
SOCKET sock = INVALID_SOCKET;
struct addrinfo *result = NULL, hints;
const char *msg = "Hello from Win32!";
WSAStartup(MAKEWORD(2,2), &wsaData);
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
getaddrinfo("example.com", "80", &hints, &result);
sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
connect(sock, result->ai_addr, (int)result->ai_addrlen);
send(sock, msg, (int)strlen(msg), 0);
closesocket(sock);
freeaddrinfo(result);
WSACleanup();
return 0;
}
Can I use
Posted by JaneDoe · 2 hrs agoWSASocketfor async I/O?What’s the best practice for handling IPv6 fallback?
Posted by devGuru · 30 mins ago