accept (Bluetooth) (Compact 2013)
3/26/2014
This function permits an incoming connection attempt on a socket.
Syntax
SOCKET accept(
SOCKET s,
struct sockaddr FAR* addr,
int FAR* addrlen
);
Parameters
- s
[in] Descriptor identifying 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 this function.
- 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 is created.
- addrlen
[in, out] Required only if addr is supplied. [in] Pointer to an integer that specifies the length of addr. The value must besizeof(SOCKADDR_BTH)
. [out] Pointer to the actual length of the returned structure pointed to by addr.
Return Value
Returns a handle to the socket on which the actual connection is made on success; otherwise returns INVALID_SOCKET. The specific error code can be retrieved by calling WSAGetLastError.
Remarks
Note
This function is actually a Winsock function. However, the information that is presented in it is specific to Bluetooth.
The Bluetooth server application calls the accept function to wait for the incoming connection. The addr parameter must point to the SOCKADDR_BTH structure.
For more information about the accept function, see accept (Windows Sockets) in the Winsock reference.
Example
The following example code shows how to use accept.
Important
For readability, the following code example does not contain security checking or error handling. Do not use the following code in a production environment.
SOCKET s = socket (AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
if (s == INVALID_SOCKET) {
wprintf (L"Socket creation failed, error %d\n", WSAGetLastError ());
return;
}
SOCKADDR_BTH sab;
memset (&sab, 0, sizeof(sab));
sab.addressFamily = AF_BTH;
if (0 != bind (s, (SOCKADDR *) &sab, sizeof(sab))) {
wprintf (L"Socket bind, error %d\n", WSAGetLastError ());
closesocket (s);
return;
}
listen (s, 5);
for ( ; ; ) {
SOCKADDR_BTH sab2;
int ilen = sizeof(sab2);
SOCKET s2 = accept (s, &sab2, &ilen);
if (s2 == INVALID_SOCKET) {
wprintf (L"Socket bind, error %d\n", WSAGetLastError ());
break;
}
wprintf (L"Connection came from %04x%08x to channel %d\n",
GET_NAP(sab2.btAddr), GET_SAP(sab2.btAddr), sab2.port);
SpinConnectionThreadsOnSocket (s2);
}
closesocket (s);
return;
Requirements
Header |
winsock2.h |
Library |
Ws2.lib |
See Also
Reference
Bluetooth API Miscellaneous Functions
SOCKADDR_BTH