connect (Bluetooth) (Compact 2013)
3/26/2014
This function establishes a connection to a specified socket.
Syntax
int connect(
SOCKET s,
const struct sockaddr FAR* name,
int namelen
);
Parameters
- s
[in] Descriptor identifying an unconnected socket.
- name
[in] Name of the socket to which the connection should be established.
- namelen
[in] Length of the name.
Return Value
Returns zero on success; otherwise SOCKET_ERROR. 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.
Use this function to connect to a target device. Socket s must be created as a Bluetooth socket. The parameter name must specify the target Bluetooth device. If name.port is 0, then name.serviceClassId should contain the UUID of an RFCOMM-based service. Winsock performs an SDP call to find out the server channel number.
Note
RFCOMM supports only one connection for a particular server channel between two devices. Which means that if application A on device X is connected to server P on device Y, application B on device X will not be able to connect to the same server on device Y. However, device Y will be able to accept an incoming connection on the same server channel from a different device, as well as an incoming connection from device X to a different server channel.
For more information about the connect function, see connect (Windows Sockets) in the Winsock reference.
Example
The following example code shows how to use the connect function to connect to a device.
SOCKET s = socket (AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
if (s == INVALID_SOCKET) {
wprintf (L"Could not create socket: error %d\n", WSAGetLastError ());
return;
}
SOCKADDR_BTH sab;
memset (&sab, 0, sizeof(sab));
sab.addressFamily = AF_BTH;
sab.serviceClassId = FaxServiceClass_UUID;
sab.btAddr = target_bluetooth_device;
if (0 != connect (s, &sab, sizeof(sab)) {
wprintf (L"Could not connect socket: error %d\n", WSAGetLastError ());
return;
}
Requirements
Header |
winsock2.h |
Library |
Ws2.lib |