This function accepts a connection on a socket.
At a Glance
Header file: | Winsock.h |
Windows CE versions: | 1.0 and later |
Syntax
SOCKET accept(SOCKET s, struct sockaddr *addr, int *addrlen);
Parameters
s
[in] 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
[out] 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 established when the socket was created. Set to NULL for IrSocket implementation.
addrlen
[out] Optional pointer to an integer that contains the length of the address addr. Set to NULL for IrSocket implementation.
Return Values
The return value is a handle for the socket on which the actual connection is made. INVALID_SOCKET indicates failure. To get a specific error value, call WSAGetLastError. The following table shows common error values.
Value | Description |
WSANOTINITIALISED | A successful WSAStartup must occur before calling accept. |
WSAENETDOWN | The network subsystem has failed. |
WSAEFAULT | The addrlen parameter is too small or addr is not a valid part of the user address space. |
WSAEINPROGRESS | A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function. |
WSAEINVAL | The listen function was not invoked prior to accept. |
WSAEMFILE | The queue is nonempty upon entry to accept and there are no descriptors available. |
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. |
Windows CE does not support the WSAEINTR error value.
The integer referred to by addrlen initially contains the amount of space pointed to by addr. On return it contains the actual length in bytes of the address returned.
Remarks
For Infrared Sockets (IrSock), set the addr and addrlen parameters to NULL.
The accept function extracts the first connection on the queue of pending connections on socket s. It then creates a new socket and returns a handle to the new socket. The newly created socket is the socket that will handle the actual connection and has the same properties as socket s. The newly created socket s does not have the listening socket’s group identifier, if any was applied.
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 nonblocking and no pending connections are present on the queue, accept returns the WSAEWOULDBLOCK error value. The socket handle returned from a successful accept call cannot be used to accept further connections. The original socket remains open to listen 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.
See Also