Skip to main content
StreamSocket.ConnectAsync(HostName, String, SocketProtectionLevel) | connectAsync(HostName, String, SocketProtectionLevel) method

Applies to Windows and Windows Phone

Starts an asynchronous operation on a StreamSocket object to connect to a remote destination specified by a remote hostname, a remote service name, and a SocketProtectionLevel.

Syntax


streamSocket.connectAsync(remoteHostName, remoteServiceName, protectionLevel).done( /* Your success and error handlers */ );

public IAsyncAction ConnectAsync(
  HostName remoteHostName, 
  string remoteServiceName, 
  SocketProtectionLevel protectionLevel
)

Public Function ConnectAsync(
  remoteHostName As HostName,  
  remoteServiceName As String,  
  protectionLevel As SocketProtectionLevel 
) As IAsyncAction

public:
IAsyncAction^ ConnectAsync(
  HostName^ remoteHostName, 
  String^ remoteServiceName, 
  SocketProtectionLevel protectionLevel
)

Parameters

remoteHostName

Type: HostName

The hostname or IP address of the remote network destination. For Bluetooth RFCOMM, this is a MAC address.

remoteServiceName

Type: String [JavaScript] | System.String [.NET] | Platform::String [C++]

The service name or TCP port number of the remote network destination. For Bluetooth RFCOMM, this is the Bluetooth address.

protectionLevel

Type: SocketProtectionLevel

The protection level that represents the integrity and encryption for the StreamSocket object.

Return value

Type: IAsyncAction

An asynchronous connect operation on a StreamSocket object.

Remarks

The remoteHostName and remoteServiceName parameters must be provided. If the remoteHostName is null or the remoteServiceName is null or an empty string, an error will occur.

When the protectionLevel parameter is set to a value that requires SSL or TLS, the socket connect operation may not timeout if the remote network destination does not support SSL or TLS. This can occur if initial connect operation succeeds but the remote host does not terminate the connection during the SSL handshake. To protect against this situation, an app should set a timeout on the connect operation when requesting SSL/TLS and abort the operation if the timeout expires. For more information on setting a timeout using JavaScript on socket operations, see How to set timeouts on socket operations (HTML). For more information on setting a timeout using VB, C#, or C++ on socket operations, see How to set timeouts on socket operations (XAML).

In a Windows Store app, the StreamSocket class supports connecting to a remote endpoint when proxies are required to complete the connection. This support for proxies is automatic and transparent to the app. For more detailed information, see the remarks on Support for proxies in the StreamSocket class reference.

In a Windows Phone Store app, the StreamSocket class does not provide automatic support for proxies.

Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8

Namespace

Windows.Networking.Sockets
Windows::Networking::Sockets [C++]

Metadata

Windows.winmd

DLL

Windows.Networking.dll

Capabilities

internetClientServer
privateNetworkClientServer
ID_CAP_NETWORKING [Windows Phone]

See also

Other resources
How to set timeouts on socket operations (HTML)
How to set timeouts on socket operations (XAML)
Reference
ConnectAsync(EndpointPair)
ConnectAsync(HostName, String)
ConnectAsync(EndpointPair, SocketProtectionLevel)
ConnectAsync(HostName, String, SocketProtectionLevel, NetworkAdapter)
HostName
SocketProtectionLevel
StreamSocket