다음을 통해 공유


Socket.ConnectAsync Method (SocketAsyncEventArgs)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Begins an asynchronous request for a remote host connection.

Namespace:  System.Net.Sockets
Assembly:  System.Net (in System.Net.dll)

Syntax

'Declaration
Public Function ConnectAsync ( _
    e As SocketAsyncEventArgs _
) As Boolean
public bool ConnectAsync(
    SocketAsyncEventArgs e
)

Parameters

Return Value

Type: System.Boolean
Returns true if the I/O operation is pending. The SocketAsyncEventArgs.Completed event on the System.Net.Sockets.SocketAsyncEventArgs object passed in the e parameter will be raised upon completion of the operation.
Returns false if the I/O operation completed synchronously. In this case, The SocketAsyncEventArgs.Completed event on the System.Net.Sockets.SocketAsyncEventArgs object passed in the e parameter will not be raised. The System.Net.Sockets.SocketAsyncEventArgs object passed in the e parameter may be examined immediately after the method call returns to retrieve the result of the operation.

Exceptions

Exception Condition
ArgumentException

An argument is not valid. This exception occurs if multiple buffers are specified, the SocketAsyncEventArgs.BufferList property is not null.

ArgumentNullException

The SocketAsyncEventArgs.RemoteEndPoint cannot be null.

InvalidOperationException

The Socket is listening or a socket operation was already in progress using the System.Net.Sockets.SocketAsyncEventArgs object passed in the e parameter.

SocketException

An error occurred when attempting to access the socket. See the Remarks section for more information.

NotSupportedException

Windows XP or later is required for this method. This exception also occurs if address family of the Socket and the SocketAsyncEventArgs.RemoteEndPoint are not the same address family.

ObjectDisposedException

The Socket has been closed.

SecurityException

A caller higher in the call stack does not have permission for the requested operation.

Remarks

If you are using a connection-oriented protocol, the ConnectAsync method starts an asynchronous request for a connection to the remote host.

To be notified of completion, you must create a callback method that implements the EventHandler<SocketAsyncEventArgs> delegate and attach the callback to the SocketAsyncEventArgs.Completed event.

The caller must set the SocketAsyncEventArgs.RemoteEndPoint property to the DnsEndPoint or IPEndPoint of the remote host to connect to.

The following properties and events on the System.Net.Sockets.SocketAsyncEventArgs object are required:

The caller may set the SocketAsyncEventArgs.UserToken property to any user state object desired before calling the ConnectAsync method, so that the information will be retrievable in the callback method. If the callback needs more information than a single object, a small class can be created to hold the other required state information as members.

Optionally, a buffer may be provided which will atomically be sent on the socket after the ConnectAsync method succeeds. In this case, the SocketAsyncEventArgs.Buffer property needs to be set to the buffer containing the data to send and the SocketAsyncEventArgs.Count property needs to be set to the number of bytes of data to send from the buffer. Once a connection is established, this buffer of data is sent.

When using a connection-oriented protocol, the underlying service provider will assign the most appropriate local network address and local port number for the connection.

The ConnectAsync method throws NotSupportedException if the address family of the Socket and the SocketAsyncEventArgs.RemoteEndPoint are not the same address family.

NoteNote:

If you receive a SocketException when calling this method, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets Error Codes documentation in the MSDN library for a detailed description of the error.

Normally, a client will initiate a connection to the host from which the web page with the XAML application was downloaded. One method that can be used to determine the host name from which the web page was downloaded is to use the System.Windows.Application class in the System.Windows namespace to get the Application.Current property of the Silverlight application. This property returns a System.Windows.Interop.SilverlightHost instance for the Silverlight application. The SilverlightHost.Source property on this instance gets the Uri used to connect to the host. The Uri.Host property gets the host component of this instance. This host component can then be used with a port number to construct a new DnsEndPoint instance by calling one of the DnsEndPoint constructors.

If the XAP file for the XAML application was loaded using a Uri of Uri.UriSchemeFile to a file on the local computer, the SilverlightHost.Source property is set to a Uri with the Uri.Scheme property set to Uri.UriSchemeFile and the Uri.Host property will not be valid for use in a System.Net.DnsEndPoint.

For sockets, the security policy system in Silverlight 2 and later affects both site-of-origin and cross-domain network access. A security policy is required for any connections from sockets, even when the connection is back to the site of origin. For more information on the security policy system in Silverlight, see Network Security Access Restrictions in Silverlight.

Examples

' Create a socket and connect to the server
Dim sock As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

AddHandler socketEventArg.Completed, AddressOf SocketEventArg_Completed

socketEventArg.RemoteEndPoint = hostEntry
socketEventArg.UserToken = sock
sock.ConnectAsync(socketEventArg)
// Create a socket and connect to the server
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(SocketEventArg_Completed);

socketEventArg.RemoteEndPoint = hostEntry;
socketEventArg.UserToken = sock;
sock.ConnectAsync(socketEventArg);

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.