The accept function permits an incoming connection attempt on a socket.
SOCKET WSAAPI accept( SOCKET s, sockaddr *addr, int *addrlen );
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.
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.
An optional pointer to an integer that contains the length of structure pointed to by the addr parameter.
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.
|A successful WSAStartup call must occur before using this function.|
|An incoming connection was indicated, but was subsequently terminated by the remote peer prior to accepting the call.|
|The addrlen parameter is too small or addr is not a valid part of the user address space.|
|A blocking Windows Sockets 1.1 call was canceled through WSACancelBlockingCall.|
|The listen function was not invoked prior to accept.|
|A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.|
|The queue is nonempty upon entry to accept and there are no descriptors available.|
|The network subsystem has failed.|
|No buffer space is available.|
|The descriptor is not a socket.|
|The referenced socket is not a type that supports connection-oriented service.|
|The socket is marked as nonblocking and no connections are present to be accepted.|
The accept function 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 the WSAAsyncSelect or WSAEventSelect functions.
The accept function 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.
The accept function 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.
#ifndef UNICODE #define UNICODE #endif
The following are important issues associated with connection setup, and must be considered when using Asynchronous Transfer Mode (ATM) with Windows Sockets 2:
- The accept and WSAAccept functions do not necessarily set the remote address and address length parameters. Therefore, when using ATM, the caller should use the WSAAccept function and place ATM_CALLING_PARTY_NUMBER_IE in the ProviderSpecific member of the QoS structure, which itself is included in the lpSQOS parameter of the callback function used in accordance with WSAAccept.
- When using the accept function, realize that the function may return before connection establishment has traversed the entire distance between sender and receiver. This is because the accept function returns as soon as it receives a CONNECT ACK message; in ATM, a CONNECT ACK message is returned by the next switch in the path as soon as a CONNECT message is processed (rather than the CONNECT ACK being sent by the end node to which the connection is ultimately established). As such, applications should realize that if data is sent immediately following receipt of a CONNECT ACK message, data loss is possible, since the connection may not have been established all the way between sender and receiver.
Windows 8.1 and Windows Server 2012 R2: This function is supported for Windows Store apps on Windows 8.1, Windows Server 2012 R2, and later.
|Minimum supported client||Windows 8.1, Windows Vista [desktop apps | UWP apps]|
|Minimum supported server||Windows Server 2003 [desktop apps | UWP apps]|