The
accept function permits an incoming connection attempt on a socket.
Syntax
SOCKET accept(
__in SOCKET s,
__out struct sockaddr *addr,
__inout int *addrlen
);
Parameters
- s [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 [out]
-
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 [in, out]
-
An optional pointer to an integer that contains the length of structure pointed to by the addr parameter.
Return Value
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.
| Error code | Meaning |
- 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
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.
Example Code
The following example demonstrates the use of the accept function.
#include <winsock2.h>
#include <stdio.h>
#include <windows.h>
#pragma comment(lib, "wininet.lib")
int main(void) {
//----------------------
// Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR) {
printf("Error at WSAStartup()\n");
return 1;
}
//----------------------
// Create a SOCKET for listening for
// incoming connection requests.
SOCKET ListenSocket;
ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ListenSocket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
//----------------------
// The sockaddr_in structure specifies the address family,
// IP address, and port for the socket that is being bound.
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("127.0.0.1");
service.sin_port = htons(27015);
if (bind( ListenSocket,
(SOCKADDR*) &service,
sizeof(service)) == SOCKET_ERROR) {
printf("bind() failed.\n");
closesocket(ListenSocket);
WSACleanup();
return 1;
}
//----------------------
// Listen for incoming connection requests.
// on the created socket
if (listen( ListenSocket, 1 ) == SOCKET_ERROR) {
printf("Error listening on socket.\n");
closesocket(ListenSocket);
WSACleanup();
return 1;
}
//----------------------
// Create a SOCKET for accepting incoming requests.
SOCKET AcceptSocket;
printf("Waiting for client to connect...\n");
//----------------------
// Accept the connection.
AcceptSocket = accept( ListenSocket, NULL, NULL );
if (AcceptSocket == INVALID_SOCKET) {
printf("accept failed: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
} else
printf("Client connected.\n");
// No longer need server socket
closesocket(ListenSocket);
WSACleanup();
return 0;
}
For another example that uses the accept function, see Getting Started With Winsock.
Notes for ATM
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.
Requirements
| Minimum supported client | Windows 2000 Professional |
| Minimum supported server | Windows 2000 Server |
| Header | Winsock2.h |
| Library | Ws2_32.lib |
| DLL | Ws2_32.dll |
See Also
- Winsock Reference
- Winsock Functions
- bind
- connect
- listen
- select
- sockaddr
- socket
- WSAAsyncSelect
- WSAAccept
Send comments about this topic to Microsoft
Build date: 11/12/2009