Export (0) Print
Expand All

Socket.BeginConnect Method (IPAddress[], Int32, AsyncCallback, Object)

Begins an asynchronous request for a remote host connection. The host is specified by an IPAddress array and a port number.

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

[HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading = true)]
public IAsyncResult BeginConnect(
	IPAddress[] addresses,
	int port,
	AsyncCallback requestCallback,
	Object state
)

Parameters

addresses
Type: System.Net.IPAddress[]

At least one IPAddress, designating the remote host.

port
Type: System.Int32

The port number of the remote host.

requestCallback
Type: System.AsyncCallback

An AsyncCallback delegate that references the method to invoke when the connect operation is complete.

state
Type: System.Object

A user-defined object that contains information about the connect operation. This object is passed to the requestCallback delegate when the operation is complete.

Return Value

Type: System.IAsyncResult
An IAsyncResult that references the asynchronous connections.

ExceptionCondition
ArgumentNullException

addresses is null.

SocketException

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

ObjectDisposedException

The Socket has been closed.

NotSupportedException

This method is valid for sockets that use InterNetwork or InterNetworkV6.

ArgumentOutOfRangeException

The port number is not valid.

ArgumentException

The length of address is zero.

InvalidOperationException

The Socket is Listening.

NoteNote:

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

The asynchronous BeginConnect operation must be completed by calling the EndConnect method. Typically, the method is invoked by the requestCallback delegate.

This method does not block until the operation is complete. To block until the operation is complete, use one of the Connect method overloads.

For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously.

NoteNote:

If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

NoteNote:

If this socket has previously been disconnected, then BeginConnect must be called on a thread that will not exit until the operation is complete. This is a limitation of the underlying provider. Also the EndPoint that is used must be different.

NoteNote:

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing.

NoteNote:

The execution context (the security context, the impersonated user, and the calling context) is cached for the asynchronous Socket methods. After the first use of a particular context (a specific asynchronous Socket method, a specific Socket instance, and a specific callback), subsequent uses of that context will see a performance improvement.

The following code example initiates an asynchronous connection attempt.

public static ManualResetEvent allDone = 
    new ManualResetEvent(false);

// handles the completion of the prior asynchronous  
// connect call. the socket is passed via the objectState  
// paramater of BeginConnect(). 
public static void ConnectCallback1(IAsyncResult ar)
{
    allDone.Set();
    Socket s = (Socket) ar.AsyncState;
    s.EndConnect(ar);
}
public:
    static ManualResetEvent* allDone = 
        new ManualResetEvent(false);

    // handles the completion of the prior asynchronous 
    // connect call. the socket is passed via the objectState 
    // paramater of BeginConnect().
    static void ConnectCallback1(IAsyncResult* ar)
    {
        allDone->Set();
        Socket* s = dynamic_cast<Socket*> (ar->AsyncState);
        s->EndConnect(ar);
    }
// Asynchronous connect, using DNS.GetHostAddresses 
public static void BeginConnect2(string host, int port)
{
    IPAddress[] IPs = Dns.GetHostAddresses(host);

    Socket s = new Socket(AddressFamily.InterNetwork,
        SocketType.Stream,
        ProtocolType.Tcp);

    allDone.Reset();

    Console.WriteLine("Establishing Connection to {0}", 
        host);
    s.BeginConnect(IPs, port, 
        new AsyncCallback(ConnectCallback1), s);

    // wait here until the connect finishes.  The callback  
    // sets allDone.
    allDone.WaitOne();

    Console.WriteLine("Connection established");
}		
// Asynchronous connect, using DNS.ResolveToAddresses
static void BeginConnect2(String* host, int port)
{
    IPAddress* IPs[] = Dns::ResolveToAddresses(host);

    Socket* s = new Socket(AddressFamily::InterNetwork,
        SocketType::Stream,
        ProtocolType::Tcp);

    allDone->Reset();

    Console::WriteLine(S"Establishing Connection to {0}", 
        host);
    s->BeginConnect(IPs, port, 
        new AsyncCallback(ConnectCallback1), s);

    // wait here until the connect finishes.  The callback 
    // sets allDone.
    allDone->WaitOne();

    Console::WriteLine(S"Connection established");
}		

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft