TcpClient.BeginConnect TcpClient.BeginConnect TcpClient.BeginConnect TcpClient.BeginConnect Method

Definition

Begins an asynchronous request for a remote host connection.

Overloads

BeginConnect(IPAddress, Int32, AsyncCallback, Object) BeginConnect(IPAddress, Int32, AsyncCallback, Object) BeginConnect(IPAddress, Int32, AsyncCallback, Object) BeginConnect(IPAddress, Int32, AsyncCallback, Object)

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

BeginConnect(IPAddress[], Int32, AsyncCallback, Object) BeginConnect(IPAddress[], Int32, AsyncCallback, Object) BeginConnect(IPAddress[], Int32, AsyncCallback, Object) BeginConnect(IPAddress[], Int32, AsyncCallback, Object)

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

BeginConnect(String, Int32, AsyncCallback, Object) BeginConnect(String, Int32, AsyncCallback, Object) BeginConnect(String, Int32, AsyncCallback, Object) BeginConnect(String, Int32, AsyncCallback, Object)

Begins an asynchronous request for a remote host connection. The remote host is specified by a host name (String) and a port number (Int32).

BeginConnect(IPAddress, Int32, AsyncCallback, Object) BeginConnect(IPAddress, Int32, AsyncCallback, Object) BeginConnect(IPAddress, Int32, AsyncCallback, Object) BeginConnect(IPAddress, Int32, AsyncCallback, Object)

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

public:
 IAsyncResult ^ BeginConnect(System::Net::IPAddress ^ address, int port, AsyncCallback ^ requestCallback, System::Object ^ state);
public IAsyncResult BeginConnect (System.Net.IPAddress address, int port, AsyncCallback requestCallback, object state);
member this.BeginConnect : System.Net.IPAddress * int * AsyncCallback * obj -> IAsyncResult
Public Function BeginConnect (address As IPAddress, port As Integer, requestCallback As AsyncCallback, state As Object) As IAsyncResult

Parameters

address
IPAddress IPAddress IPAddress IPAddress

The IPAddress of the remote host.

port
Int32 Int32 Int32 Int32

The port number of the remote host.

requestCallback
AsyncCallback AsyncCallback AsyncCallback AsyncCallback

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

state
Object Object Object 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.

Returns

An IAsyncResult object that references the asynchronous connection.

Exceptions

An error occurred when attempting to access the socket.

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

Examples

The following code example creates a TcpClient and connects to a remote host.

   static void DoBeginConnect1( String^ host, int port )
   {
      // Connect asynchronously to the specifed host.
      TcpClient^ t = gcnew TcpClient( AddressFamily::InterNetwork );
//      IPAddress^ remoteHost = gcnew IPAddress( host );
      array<IPAddress^>^ remoteHost = Dns::GetHostAddresses( host );
      connectDone->Reset();
      Console::WriteLine( "Establishing Connection to {0}", host );
      t->BeginConnect( remoteHost, port, gcnew AsyncCallback( &ConnectCallback ), t );

      // Wait here until the callback processes the connection.
      connectDone->WaitOne();
      Console::WriteLine( "Connection established" );
   }
        public static void DoBeginConnect1(string host, int port)
        {
            // Connect asynchronously to the specifed host.
            TcpClient t = new TcpClient(AddressFamily.InterNetwork);
//            IPAddress remoteHost = new IPAddress(host);
            IPAddress[] remoteHost = Dns.GetHostAddresses(host);

            connectDone.Reset();

            Console.WriteLine("Establishing Connection to {0}", 
                remoteHost[0]);
            t.BeginConnect(remoteHost[0], port, 
                new AsyncCallback(ConnectCallback), t);

            // Wait here until the callback processes the connection.
            connectDone.WaitOne();

            Console.WriteLine("Connection established");
        }

Remarks

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

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

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

BeginConnect(IPAddress[], Int32, AsyncCallback, Object) BeginConnect(IPAddress[], Int32, AsyncCallback, Object) BeginConnect(IPAddress[], Int32, AsyncCallback, Object) BeginConnect(IPAddress[], Int32, AsyncCallback, Object)

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

public:
 IAsyncResult ^ BeginConnect(cli::array <System::Net::IPAddress ^> ^ addresses, int port, AsyncCallback ^ requestCallback, System::Object ^ state);
public IAsyncResult BeginConnect (System.Net.IPAddress[] addresses, int port, AsyncCallback requestCallback, object state);
member this.BeginConnect : System.Net.IPAddress[] * int * AsyncCallback * obj -> IAsyncResult
Public Function BeginConnect (addresses As IPAddress(), port As Integer, requestCallback As AsyncCallback, state As Object) As IAsyncResult

Parameters

addresses
IPAddress[]

At least one IPAddress that designates the remote hosts.

port
Int32 Int32 Int32 Int32

The port number of the remote hosts.

requestCallback
AsyncCallback AsyncCallback AsyncCallback AsyncCallback

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

state
Object Object Object 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.

Returns

An IAsyncResult object that references the asynchronous connection.

Exceptions

An error occurred when attempting to access the socket.

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

Examples

The following code example creates a TcpClient and connects to a remote host.

// Connect asynchronously to the specifed host.
static void DoBeginConnect2( String^ host, int port )
{
   TcpClient^ t = gcnew TcpClient( AddressFamily::InterNetwork );
   array<IPAddress^>^remoteHost = Dns::GetHostAddresses( host );
   connectDone->Reset();
   Console::WriteLine( "Establishing Connection to {0}", host );
   t->BeginConnect( remoteHost, port, gcnew AsyncCallback(  &ConnectCallback ), t );

   // Wait here until the callback processes the connection.
   connectDone->WaitOne();
   Console::WriteLine( "Connection established" );
}
// Connect asynchronously to the specifed host.
public static void DoBeginConnect2(string host, int port)
{
    TcpClient t = new TcpClient(AddressFamily.InterNetwork);
    IPAddress[] remoteHost = Dns.GetHostAddresses(host);

    connectDone.Reset();

    Console.WriteLine("Establishing Connection to {0}", host);
    t.BeginConnect(remoteHost, port, 
        new AsyncCallback(ConnectCallback), t);

    // Wait here until the callback processes the connection.
    connectDone.WaitOne();

    Console.WriteLine("Connection established");
}

Remarks

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

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

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

This method is typically used immediately after a call to the BeginGetHostAddresses method, which can return multiple IP addresses for a single host.

BeginConnect(String, Int32, AsyncCallback, Object) BeginConnect(String, Int32, AsyncCallback, Object) BeginConnect(String, Int32, AsyncCallback, Object) BeginConnect(String, Int32, AsyncCallback, Object)

Begins an asynchronous request for a remote host connection. The remote host is specified by a host name (String) and a port number (Int32).

public:
 IAsyncResult ^ BeginConnect(System::String ^ host, int port, AsyncCallback ^ requestCallback, System::Object ^ state);
public IAsyncResult BeginConnect (string host, int port, AsyncCallback requestCallback, object state);
member this.BeginConnect : string * int * AsyncCallback * obj -> IAsyncResult
Public Function BeginConnect (host As String, port As Integer, requestCallback As AsyncCallback, state As Object) As IAsyncResult

Parameters

host
String String String String

The name of the remote host.

port
Int32 Int32 Int32 Int32

The port number of the remote host.

requestCallback
AsyncCallback AsyncCallback AsyncCallback AsyncCallback

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

state
Object Object Object 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.

Returns

An IAsyncResult object that references the asynchronous connection.

Exceptions

An error occurred when attempting to access the socket.

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

Examples

The following code example creates a TcpClient and connects to a remote host.

// Connect asynchronously to the specifed host.
static void DoBeginConnect3( String^ host, int port )
{
   TcpClient^ t = gcnew TcpClient( AddressFamily::InterNetwork );
   connectDone->Reset();
   Console::WriteLine( "Establishing Connection to {0}", host );
   t->BeginConnect( host, port, gcnew AsyncCallback(  &ConnectCallback ), t );
   
   // Wait here until the callback processes the connection.
   connectDone->WaitOne();
   Console::WriteLine( "Connection established" );
}
// Connect asynchronously to the specifed host.
public static void DoBeginConnect3(string host, int port)
{
    TcpClient t = new TcpClient(AddressFamily.InterNetwork);

    connectDone.Reset();

    Console.WriteLine("Establishing Connection to {0}", 
        host);
    t.BeginConnect(host, port, 
        new AsyncCallback(ConnectCallback), t);

    // Wait here until the callback processes the connection.
    connectDone.WaitOne();

    Console.WriteLine("Connection established");
}

Remarks

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

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

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

Applies to