Socket

A socket is an endpoint for communication. An application creates a socket to establish a connection with another socket. Each socket is associated with a particular transport protocol and address.

The Windows Sockets API (Winsock) provides a programming interface for network applications that communicate over TCP/IP. It is based on the Berkeley sockets API.

Socket Types

Sockets are typically created with a specific type that determines the communication semantics. Common socket types include:

  • SOCK_STREAM: Provides a reliable, two-way, connection-based byte stream. TCP is an example of a transport protocol that provides SOCK_STREAM semantics.
  • SOCK_DGRAM: Provides a fixed-length, connectionless datagram. UDP is an example of a transport protocol that provides SOCK_DGRAM semantics.
  • SOCK_RAW: Provides raw network protocol access. This is typically used for protocols like ICMP.

Socket Creation

The primary function for creating a socket is socket(). This function requires the following parameters:

  • af: The address family. For the Internet Protocol version 4 (IPv4), this is AF_INET. For IPv6, it's AF_INET6.
  • type: The socket type, such as SOCK_STREAM or SOCK_DGRAM.
  • protocol: The specific protocol to be used with the socket. This is often set to 0 to allow the system to choose the default protocol for the given address family and socket type.

int socket(
  int af,
  int type,
  int protocol
);
                

Upon successful creation, socket() returns a descriptor for the new socket. If an error occurs, it returns INVALID_SOCKET.

Socket Operations

Once a socket is created, various operations can be performed on it, including:

  • Binding: Associating a socket with a specific local address and port.
  • Listening: Preparing a connection-oriented socket to accept incoming connection requests.
  • Connecting: Establishing a connection to a remote socket for connection-oriented protocols.
  • Sending/Receiving: Transmitting and receiving data over the socket.
  • Closing: Releasing the socket and its associated resources.
Always check the return values of Winsock functions to handle errors appropriately.

Example: Creating a TCP Socket

The following C++ code snippet demonstrates how to create a TCP socket:


#include <winsock2.h>

// Link with Ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")

int main() {
    WSADATA wsaData;
    int iResult;

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }

    SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (clientSocket == INVALID_SOCKET) {
        printf("Error creating socket: %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    printf("Socket created successfully.\n");

    // ... further socket operations ...

    closesocket(clientSocket);
    WSACleanup();

    return 0;
}
                

Related Topics