accept Function (Winsock)

The accept function retrieves the first connection pending on the message queue of a listening socket.

Syntax

SOCKET accept(
  _In_      SOCKET   ListenSocket,
  _Out_opt_ sockaddr *Addr,
  _Inout_opt_ int      *AddrSize
);

Parameters

Parameter Description
ListenSocket

A descriptor identifying the socket that is listening for connections. This socket must be bound to a specific address and in a listening state.

Addr

An optional pointer to a buffer that receives the address structure of the connecting party. The structure of the address is determined by the address family specified in the ListenSocket.

For the AF_INET address family, the Addr parameter should be a pointer to a sockaddr_in structure.

For the AF_INET6 address family, the Addr parameter should be a pointer to a sockaddr_in6 structure.

If this parameter is NULL, the information about the connecting party's address is not returned.

AddrSize

An optional pointer to an integer that specifies the size, in bytes, of the buffer pointed to by the Addr parameter. When the function returns, this parameter contains the actual size of the buffer occupied by the address information. This is the size of the structure pointed to by Addr.

If Addr is NULL, this parameter must be NULL.

Return Value

If no error occurs, accept returns a new socket descriptor that is connected to a socket on the server. This descriptor is a value whose further network-related operations refer to that communications path.

If an I/O error occurs before any connections are made, the accept function returns INVALID_SOCKET. For more information about error codes, see Winsock Error Codes.

Remarks

The accept function establishes a connection with a first-queued request in the pending connection queue of a socket that is in a listening state (on which listen has been called). If no pending connection is present, the function blocks until a connection is made available.

If the socket is not in a listening state, accept returns an error.

When accept returns successfully, the socket it returns is a fully connected socket ready for sending and receiving data. The original socket, specified by ListenSocket, remains in a listening state.

The Addr parameter is an output parameter that is filled with the address of the party that is connecting to the socket. The AddrSize parameter is an input/output parameter that specifies the size of the buffer allocated for Addr.

For connection-oriented protocols, accept is used to create a new socket descriptor for each connection that is made.

The socket returned by accept has the same socket properties as the listening socket.

If the listening socket is in blocking mode, accept will block until a connection request is received.

If the listening socket is in non-blocking mode, accept will return SOCKET_ERROR if no connection is pending. WSAGetLastError can be called to retrieve extended error information.

The returned socket descriptor is a duplicate of the listening socket, but it is a new descriptor. It has the same socket options as the listening socket. The accept function can be called multiple times to handle multiple simultaneous connections.

The Addr buffer is filled with the socket address of the incoming connection. The AddrSize parameter indicates the size of the returned socket address.