Microsoft Learn

Accepting Connections

This document describes how to use the accept() function in Winsock to accept incoming connection requests on a listening socket.

Overview of Accepting Connections

Establishing a network connection is a fundamental aspect of many client-server applications. Winsock provides the accept() function as the primary mechanism for a server to handle incoming connection requests from clients. When a client attempts to connect to a server that is listening on a specific socket, the accept() function allows the server to create a new socket for that specific client communication.

The accept() Function

The accept() function is used to retrieve a connection request on a socket that is in a listening state. When a connection is available, accept() creates a new socket that is connected to the client and returns a handle to this new socket.

Syntax

SOCKET accept(
  [in]      SOCKET    ListenSocket,
  [out]     sockaddr  *Addr,
  [in, out] int       *AddrLen
);

Parameters

  • ListenSocket: [in] A descriptor identifying a socket that is bound to a specific transport address and is listening for connection attempts.
  • Addr: [out] An optional pointer to a buffer that will receive the address structure of the connecting entity.
  • AddrLen: [in, out] A pointer to an integer value that specifies the size, in bytes, of the buffer pointed to by the Addr parameter.

Return Value

If no error occurs, accept() returns a descriptor for a newly created socket that is connected to (a peer). This returned socket is ready for sending and receiving data. If an error occurs, the function returns INVALID_SOCKET, and a specific error code can be retrieved by calling WSAGetLastError().

Steps to Accept a Connection

  1. Create a Listening Socket: Use socket() to create a TCP socket.
  2. Bind the Socket: Use bind() to associate the socket with a local address and port.
  3. Listen for Connections: Use listen() to put the socket into a listening state.
  4. Accept an Incoming Connection: Call accept(). This call will block until a client connection is established.
  5. Communicate with the Client: Use the new socket returned by accept() to send and receive data with the connected client using functions like send() and recv().
  6. Close the Client Socket: When communication is complete, close the client socket using closesocket().

Example Usage (Conceptual)

The following C++ snippet illustrates the basic flow of using accept():

#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")

// ... (Initialization code for Winsock)

SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverSocket == INVALID_SOCKET) {
    // Handle error
}

SOCKADDR_IN serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8080); // Example port
serverAddr.sin_addr.s_addr = INADDR_ANY;

if (bind(serverSocket, (SOCKADDR*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
    // Handle error
}

if (listen(serverSocket, SOMAXCONN) == SOCKET_ERROR) {
    // Handle error
}

SOCKADDR_IN clientAddr;
int clientAddrSize = sizeof(clientAddr);
SOCKET clientSocket = accept(serverSocket, (SOCKADDR*)&clientAddr, &clientAddrSize);

if (clientSocket == INVALID_SOCKET) {
    // Handle error
} else {
    // Successfully accepted a connection
    // Communicate with clientSocket here
    closesocket(clientSocket); // Close the client socket when done
}

closesocket(serverSocket); // Close the listening socket
// ... (Cleanup code for Winsock)

Important Considerations