accept

The accept function permits an incoming connection attempt on a socket.

Syntax

SOCKET accept(
         SOCKET s,
         sockaddr *addr,
         int *addrlen
)  

Parameters

s
Type: SOCKET 

[in] A descriptor that identifies a socket that has been placed in a listening state with the listen function. The connection is actually made with the socket that is returned by accept.

*addr
Type: sockaddr 

[out, optional] An optional pointer to a buffer that receives the address of the connecting entity, as known to the communications layer. The exact format of the addr parameter is determined by the address family that was established when the socket from the sockaddr structure was created.

*addrlen
Type: int 

[in, out, optional] An optional pointer to an integer that contains the length of structure pointed to by the addr parameter.

Return value

Type: SOCKET 

If no error occurs, accept returns a value of type SOCKET that is a descriptor for the new socket. This returned value is a handle for the socket on which the actual connection is made.

Otherwise, a value of INVALID_SOCKET is returned, and a specific error code can be retrieved by calling WSAGetLastError.

The integer referred to by addrlen initially contains the amount of space pointed to by addr. On return it will contain the actual length in bytes of the address returned.

Return codes
Return code Description
WSANOTINITIALISED A successful WSAStartup call must occur before using this function.
WSAECONNRESET An incoming connection was indicated, but was subsequently terminated by the remote peer prior to accepting the call.
WSAEFAULT The addrlen parameter is too small or addr is not a valid part of the user address space.
WSAEINTR A blocking Windows Sockets 1.1 call was canceled through WSACancelBlockingCall.
WSAEINVAL The listen function was not invoked prior to accept.
WSAEINPROGRESS A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.
WSAEMFILE The queue is nonempty upon entry to accept and there are no descriptors available.
WSAENETDOWN The network subsystem has failed.
WSAENOBUFS No buffer space is available.
WSAENOTSOCK The descriptor is not a socket.
WSAEOPNOTSUPP The referenced socket is not a type that supports connection-oriented service.
WSAEWOULDBLOCK The socket is marked as nonblocking and no connections are present to be accepted.

Remarks

accept extracts the first connection on the queue of pending connections on socket s. It then creates and returns a handle to the new socket. The newly created socket is the socket that will handle the actual connection; it has the same properties as socket s, including the asynchronous events registered with WSAEventSelect.

accept can block the caller until a connection is present if no pending connections are present on the queue, and the socket is marked as blocking. If the socket is marked as nonblocking and no pending connections are present on the queue, accept returns an error as described in the following. After the successful completion of accept returns a new socket handle, the accepted socket cannot be used to accept more connections. The original socket remains open and listens for new connection requests.

The parameter addr is a result parameter that is filled in with the address of the connecting entity, as known to the communications layer. The exact format of the addr parameter is determined by the address family in which the communication is occurring. The addrlen is a value-result parameter; it should initially contain the amount of space pointed to by addr; on return it will contain the actual length (in bytes) of the address returned.

accept is used with connection-oriented socket types such as SOCK_STREAM. If addr and/or addrlen are equal to NULL, then no information about the remote address of the accepted socket is returned.

Note

When issuing a blocking Winsock call such as accept, Winsock may need to wait for a network event before the call can complete. Winsock performs an alertable wait in this situation, which can be interrupted by an asynchronous procedure call (APC) scheduled on the same thread. Issuing another blocking Winsock call inside an APC that interrupted an ongoing blocking Winsock call on the same thread will lead to undefined behavior, and must never be attempted by Winsock clients.

Example Code

For an example that uses accept, see Getting Started With Winsock.

Requirements

Header: Declared in winsock2.h.

Library: Use ws2_32.lib.