Introduction to Sockets
Sockets provide a mechanism for interprocess communication (IPC) and network communication. They act as endpoints for sending and receiving data across a network. The socket API, often referred to as the Berkeley Sockets API, is a standard interface for network programming that has been adopted by many operating systems, including Windows through the Winsock API.
A socket can be thought of as an abstraction that allows applications to use network protocols like TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) without needing to understand the intricate details of the underlying network stack.
Socket Types
Sockets are typically characterized by their communication domain and socket type.
The most common domain is the Internet domain (AF_INET for IPv4, AF_INET6 for IPv6).
The primary socket types are:
- Stream Sockets (
SOCK_STREAM): Provide a reliable, ordered, and bidirectional byte stream. TCP is the protocol typically used with stream sockets. Data is sent as a stream of bytes, and the protocol ensures delivery and order. - Datagram Sockets (
SOCK_DGRAM): Provide a fixed-size message, connectionless service. UDP is the protocol typically used with datagram sockets. Data is sent in discrete packets (datagrams), and delivery is not guaranteed.
Address Structures
To communicate, sockets need addresses that specify the network interface, port number, and IP address. Common structures include:
sockaddr_in(for IPv4)sockaddr_in6(for IPv6)
These structures typically contain:
struct sockaddr_in {
sa_family_t sin_family; /* Address family: AF_INET */
in_port_t sin_port; /* Port number */
struct in_addr sin_addr; /* Internet address */
char sin_zero[8]; /* Not used */
};
Creating a Socket
The socket() function is used to create a new socket.
AF_INET for IPv4).SOCK_STREAM or SOCK_DGRAM).Connecting
For connection-oriented sockets (like SOCK_STREAM), connect() establishes a connection to a remote host.
socket().sockaddr structure specifying the remote address.Listening
Server applications use bind() to associate a socket with a specific local address and port,
and then listen() to prepare the socket to accept incoming connections.
sockaddr structure specifying the local address and port.Accepting Connections
The accept() function is used by a server to retrieve a pending connection request.
It creates a new socket for the communication with the client.
sockaddr structure to store the client's address.Sending and Receiving Data
Data is exchanged using send() (or write()) and recv() (or read()) for stream sockets,
or sendto() and recvfrom() for datagram sockets.
Closing a Socket
The close() (or closesocket() in Winsock) function deallocates the socket descriptor and frees associated resources.
Full API Reference (Key Functions)
This section provides a brief overview of commonly used socket functions. For a complete list and detailed parameters, please refer to the platform-specific Winsock documentation.
Initialization and Cleanup (Winsock Specific)
closesocket returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error number may be retrieved by calling WSAGetLastError.
Socket Creation and Configuration
AF_INET, AF_INET6).SOCK_STREAM, SOCK_DGRAM).socket returns a value of type SOCKET which is a descriptor for the new socket. Otherwise, SOCKET_ERROR is returned.
sockaddr structure that specifies the local address and port for the socket.sockaddr structure.bind returns zero. Otherwise, a value of SOCKET_ERROR is returned.
listen returns zero. Otherwise, a value of SOCKET_ERROR is returned.
accept returns a descriptor for a newly created socket that is connected to s through the specified address. Otherwise, SOCKET_ERROR is returned.
Data Transfer
sockaddr structure that specifies the remote address and port for the connection.sockaddr structure.connect returns zero. Otherwise, it returns SOCKET_ERROR.
send returns the total number of bytes sent. Otherwise, a value of SOCKET_ERROR is returned.
recv returns the number of bytes received. If the connection has been gracefully closed, the return value is zero. Otherwise, a value of SOCKET_ERROR is returned.
sockaddr structure specifying the destination address.sockaddr structure that will receive the address of the sending entity.