Network I/O
This section provides comprehensive documentation on the Windows Sockets API (Winsock), a set of functions that allow applications to develop Internet and network-aware applications. Winsock provides a standard interface for network programming, enabling applications to communicate over various network protocols.
Core Concepts
Understanding the fundamental concepts of network programming is crucial for effectively using the Windows Sockets API.
Sockets
A socket is an endpoint for communication. It's an abstract representation of one end of a connection between two programs on a network. Applications use sockets to send and receive data.
TCP/IP
Transmission Control Protocol/Internet Protocol (TCP/IP) is the most widely used protocol suite for network communication. TCP provides a reliable, connection-oriented stream of data, while IP handles the addressing and routing of packets.
UDP
User Datagram Protocol (UDP) is a simpler, connectionless protocol. It offers faster transmission than TCP but does not guarantee delivery, ordering, or error checking of packets.
Protocols
Winsock supports various network protocols, including:
- TCP (SOCK_STREAM): Connection-oriented, reliable data stream.
- UDP (SOCK_DGRAM): Connectionless, unreliable datagram service.
- Raw Sockets: Allow direct access to IP and ICMP protocols.
API Functions
The Windows Sockets API provides a rich set of functions for managing network communications.
Socket Creation
The socket() function is used to create a socket. It takes parameters for address family, socket type, and protocol.
int socket(
int af,
int type,
int protocol
);
Binding
The bind() function associates a local address (IP address and port number) with a socket.
int bind(
SOCKET s,
const struct sockaddr *name,
int namelen
);
Listening
For connection-oriented sockets (like TCP), listen() puts a socket into a listening state, preparing it to accept incoming connections.
int listen(
SOCKET s,
int backlog
);
Connecting
A client application uses connect() to establish a connection to a server socket.
int connect(
SOCKET s,
const struct sockaddr *name,
int namelen
);
Sending & Receiving
Data is transmitted and received using functions like:
send()andrecv()for basic blocking operations.sendto()andrecvfrom()for datagram sockets (UDP).WSASend()andWSARecv()for asynchronous operations.
Closing Sockets
The closesocket() function closes an existing socket.
int closesocket(
SOCKET s
);
Error Handling
Winsock functions return specific error codes that can be retrieved using WSAGetLastError().
WSAGetLastError() to diagnose issues.
Advanced Topics
Overlapped I/O
Overlapped I/O allows an application to initiate multiple I/O operations concurrently without blocking. This is achieved by using event objects and the OVERLAPPED structure.
I/O Completion Ports (IOCP)
I/O Completion Ports provide a highly scalable and efficient mechanism for handling I/O operations. They are particularly useful for server applications that need to manage a large number of concurrent connections.
WSARecv/WSASend
These functions are part of the Winsock 2 API and are fundamental to implementing asynchronous network operations. They enable applications to post I/O requests and be notified when they complete, allowing for greater responsiveness.