Microsoft Learn

Socket Creation and Management

This section covers the fundamental functions for creating, configuring, connecting, and closing sockets using the Windows Sockets API (Winsock). Understanding these functions is crucial for developing network-aware applications.

socket()

Creates a socket that is bound to a specific transport-service provider.

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

af: The address family specification. For TCP/IP, this is AF_INET or AF_INET6.

type: The socket type specification. SOCK_STREAM for connection-oriented sockets (e.g., TCP) or SOCK_DGRAM for datagram sockets (e.g., UDP).

protocol: The protocol to be used with the socket. Usually, it's 0 to let the system choose the default protocol for the specified address family and socket type.

Return Value

If no error occurs, socket() returns a descriptor for the newly created socket. Otherwise, it returns INVALID_SOCKET. To get extended error information, call WSAGetLastError().

Remarks

The socket() function is the starting point for all network communications. It allocates a socket descriptor and associates it with a particular network protocol family and type. After a socket is created, it is in an unbound state.

bind()

Associates a local address with a socket.

int bind(SOCKET s, const struct sockaddr *name, int namelen);
Parameters

s: Descriptor identifying an unbound socket.

name: Pointer to a sockaddr structure that specifies the address to associate with the socket. The structure should contain the desired local IP address and port number.

namelen: The length, in bytes, of the structure pointed to by the name parameter.

Return Value

If no error occurs, bind() returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError().

Remarks

The bind() function is used to assign a local name, consisting of a network address and a port number, to a socket. For server applications, this is essential for clients to connect to the correct service. For client applications, binding is often optional as the system can automatically assign an ephemeral port.

connect()

Establishes a connection to a remote socket.

int connect(SOCKET s, const struct sockaddr *name, int namelen);
Parameters

s: Descriptor identifying an unconnected socket.

name: Pointer to a sockaddr structure that specifies the remote address and port to connect to.

namelen: The length, in bytes, of the structure pointed to by the name parameter.

Return Value

If the connection is successful, connect() returns zero. If the connection attempt would block, the Winsock implementation performs a blocking connection attempt. If the connection fails, connect() returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError().

Remarks

For connection-oriented sockets (like TCP), connect() initiates the three-way handshake with the remote host. For connectionless sockets (like UDP), this function establishes a default destination address for subsequent datagrams sent with send().

listen()

Places a socket in a state in which it is listening for incoming connection requests.

int listen(SOCKET s, int backlog);
Parameters

s: Descriptor identifying a socket that has been bound to an address and is ready to accept connections.

backlog: The maximum length of the queue of pending connections.

Return Value

If no error occurs, listen() returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError().

Remarks

The listen() function is used by server applications. It specifies the maximum number of pending connections that can be queued up on the socket. When a connection request arrives, it is placed in the queue until accepted by accept().

accept()

Accepts a connection from a remote socket.

SOCKET accept(SOCKET s, struct sockaddr *addr, int *addrlen);
Parameters

s: Descriptor identifying a socket that is listening for connections.

addr: (Optional) Pointer to a buffer that will receive the address of the connecting entity. The structure pointed to by addr is a sockaddr structure.

addrlen: (Optional) Pointer to an integer that contains the size, in bytes, of the buffer pointed to by the addr parameter. Upon return, the addrlen parameter is updated to contain the actual length of the address returned.

Return Value

If no error occurs, accept() returns a handle to a new socket that is created for the communication with the remote host. This new socket is connected to the remote host. Otherwise, accept() returns INVALID_SOCKET. To get extended error information, call WSAGetLastError().

Remarks

The accept() function extracts the first connection request from the queue of pending connections on a listening socket. The created socket is a copy of the original socket but is connected to the client. The original listening socket remains open and continues to accept connections.

closesocket()

Closes an existing socket.

int closesocket(SOCKET s);
Parameters

s: Descriptor identifying the socket to be closed.

Return Value

If no error occurs, closesocket() returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError().

Remarks

The closesocket() function deallocates the socket descriptor and releases any resources associated with the socket. For TCP sockets, it also initiates a graceful connection termination sequence.