Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

This function conditionally accepts a connection based on the return value of a condition function and allows the transfer of connection data.

Syntax

SOCKET WSAAccept(
  SOCKET 
s,
  struct sockaddr FAR* addr,
  LPINT 
addrlen,
  LPCONDITIONPROC 
lpfnCondition,
  DWORD 
dwCallbackData
);

Parameters

s

[in] Descriptor identifying a socket that is listening for connections after a call to the listenfunction.

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 addrparameter is determined by the address family established when the socket was created.

addrlen

[in, out] Optional pointer to an integer that contains the length of the address addr.

lpfnCondition

[in] Procedure instance address of the optional, application-supplied condition function that will make an accept/reject decision based on the caller information passed in as parameters.

dwCallbackData

[in] Callback data passed back to the application as the value of the dwCallbackDataparameter of the condition function. This parameter is not interpreted by Windows Sockets.

Return Value

If no error occurs, this function returns a value of type SOCKETthat is a descriptor for the accepted socket. If an error occurs, a value of INVALID_SOCKET is returned, and a specific error code can be retrieved by calling WSAGetLastError.

The integer referred to by addrleninitially contains the amount of space pointed to by addr. On return it will contain the actual length in bytes of the address returned. The following table shows a list of possible error codes.

Error code Description

WSANOTINITIALISED

A successful WSAStartupcall must occur before using this function.

WSAECONNREFUSED

The connection request was forcefully rejected as indicated in the return value of the condition function (CF_REJECT).

WSAENETDOWN

The network subsystem has failed.

WSAEFAULT

The addrlenparameter is too small or the addror lpfnConditionare not part of the user address space.

WSAEINTR

The socket was closed.

WSAEINPROGRESS

A blocking Winsock call is in progress.

WSAEINVAL

The listenfunction was not invoked prior to this function, the return value of the condition function is not a valid one, or any case where the specified socket is in an invalid state.

WSAEMFILE

The queue is nonempty on entry to this function and there are no socket 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.

WSATRY_AGAIN

The acceptance of the connection request was deferred as indicated in the return value of the condition function (CF_DEFER).

WSAEWOULDBLOCK

The socket is marked as nonblocking and no connections are present to be accepted.

WSAEACCES

The connection request that was offered has timed out or been withdrawn.

Remarks

This function extracts the first connection on the queue of pending connections on socket sand checks it against the condition function, provided the condition function is specified (that is, not NULL). If the condition function returns CF_ACCEPT, WSAAcceptcreates a new socket. The newly created socket has the same properties as socket s,including asynchronous events registered with WSAEventSelect. If the condition function returns CF_REJECT, WSAAcceptrejects the connection request. The condition function runs in the same thread as this function and should return as soon as possible. If the decision cannot be made immediately, the condition function should return CF_DEFER to indicate that no decision has been made and no action about this connection request should be taken by the service provider. When the application is ready to take action on the connection request, it will invoke WSAAcceptagain and return either CF_ACCEPT or CF_REJECT as a return value from the condition function.

A socket in default mode (blocking) will block until a connection is present when an application calls WSAAcceptand no connections are pending on the queue.

A socket in nonblocking mode fails with the error WSAEWOULDBLOCK when an application calls WSAAcceptand no connections are pending on the queue. After WSAAcceptsucceeds and returns a new socket handle, the new socket cannot be used to accept any more connections. The original socket remains open and listens for new connection requests.

The addrparameter is a result parameter that is completed with the address of the connecting entity, as known to the communications layer. The exact format of the addrparameter is determined by the address family in which the communication is occurring. The addrlenparameter 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. This call is used with connection-oriented socket types such as SOCK_STREAM. If addrand/or addrlenare equal to NULL, then no information about the remote address of the accepted socket is returned. Otherwise, these two parameters will be completed regardless of whether the condition function is specified or what it returns.

The prototype of the callback function is as follows:

Copy Code
int CALLBACK 
ConditionFunc(
  IN LPWSABUF lpCallerId,
  IN LPWSABUF lpCallerData,
  IN OUT LPQOS lpSQOS,
  IN OUT LPQOS lpGQOS,
  IN LPWSABUF lpCalleeId,
  OUT LPWSABUF lpCalleeData,
  OUT GROUP FAR *g,
  IN DWORD_PTR dwCallbackData
);

The ConditionFuncfunction is a placeholder for the application-supplied callback function. The actual condition function must reside in a DLL or application module. It is exported in the module definition file.

The lpCallerIdparameter points to a WSABUFstructure that contains the address of the connecting entity, where its lenparameter is the length of the buffer in bytes and its bufparameter is a pointer to the buffer. The lpCallerDataparameter is a value parameter that contains any user data. The information in these parameters is sent along with the connection request. If no caller identification or caller data is available, the corresponding parameters will be NULL. Many network protocols do not support connect-time caller data. Most conventional network protocols can be expected to support caller identifier information at connection-request time. The bufportion of the WSABUFstructure pointed to by lpCallerIdpoints to a sockaddrstructure. The sockaddrstructure is interpreted according to its address family (typically by casting the sockaddrto some type specific to the address family).

The lpCalleeIdparameter is a value parameter that contains the local address of the connected entity. The bufportion of the WSABUFstructure pointed to by lpCalleeIdpoints to a sockaddrstructure. The sockaddrstructure is interpreted according to its address family (typically by casting the sockaddrto some type specific to the address family).

The lpCalleeDataparameter is a result parameter used by the condition function to supply user data back to the connecting entity. The lpCalleeData-> leninitially contains the length of the buffer allocated by the service provider and pointed to by lpCalleeData-> buf. A value of zero means passing user data back to the caller is not supported. The condition function should copy up to lpCalleeData-> lenbytes of data into lpCalleeData-> bufand then update lpCalleeData-> lento indicate the actual number of bytes transferred. If no user data is to be passed back to the caller, the condition function should set lpCalleeData-> lento zero. The format of all address and user data is specific to the address family to which the socket belongs.

The dwCallbackDataparameter value passed to the condition function is the value passed as the dwCallbackDataparameter in the original WSAAcceptcall. This value is interpreted only by the Winsock client. This allows a client to pass some context information from the WSAAcceptcall site through to the condition function. This also provides the condition function with any additional information required to determine whether to accept the connection. A typical usage is to pass a (suitably cast) pointer to a data structure containing references to application-defined objects with which this socket is associated.

Requirements

Header winsock2.h
Library Ws2.lib
Windows Embedded CE Windows CE .NET 4.0 and later
Windows Mobile Windows Mobile Version 5.0 and later

See Also