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 establishes a connection to a specified socket.

Note:
This function is actually a Winsock function. However, the information that is presented in it is specific to Bluetooth.

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

If no error occurs, this function returns zero. If an error occurs, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError.

Remarks

Use this function to connect to a target device. Socket smust be created as a Bluetooth socket. The parameter namemust specify the target Bluetooth device. If name.portis 0, then name.serviceClassIdshould contain the UUID of an RFCOMM-based service. Winsock performs an SDP call to find out the server channel number.

The following example code shows how to use the connectfunction to connect to a target device.

Copy Code
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;
}
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 connectfunction, see connect (Windows Sockets)in the Winsock reference.

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