TcpClient Constructors
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Initializes a new instance of the TcpClient class.
Overloads
TcpClient() |
Initializes a new instance of the TcpClient class. |
TcpClient(IPEndPoint) |
Initializes a new instance of the TcpClient class and binds it to the specified local endpoint. |
TcpClient(AddressFamily) |
Initializes a new instance of the TcpClient class with the specified family. |
TcpClient(String, Int32) |
Initializes a new instance of the TcpClient class and connects to the specified port on the specified host. |
TcpClient()
- Source:
- TCPClient.cs
- Source:
- TCPClient.cs
- Source:
- TCPClient.cs
Initializes a new instance of the TcpClient class.
public:
TcpClient();
public TcpClient ();
Public Sub New ()
Examples
The following code example demonstrates how to use the parameterless constructor to create a new TcpClient.
//Creates a TCPClient using the default constructor.
TcpClient^ tcpClientC = gcnew TcpClient;
//Creates a TCPClient using the default constructor.
TcpClient tcpClientC = new TcpClient ();
'Creates a TCPClient using the default constructor.
Dim tcpClientC As New TcpClient
Remarks
This constructor creates a new TcpClient and allows the underlying service provider to assign the most appropriate local IP address and port number. You must first call the Connect method before sending and receiving data.
Note
On .NET Framework, this constructor works only with IPv4 address types.
Note
This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.
See also
Applies to
TcpClient(IPEndPoint)
- Source:
- TCPClient.cs
- Source:
- TCPClient.cs
- Source:
- TCPClient.cs
Initializes a new instance of the TcpClient class and binds it to the specified local endpoint.
public:
TcpClient(System::Net::IPEndPoint ^ localEP);
public TcpClient (System.Net.IPEndPoint localEP);
new System.Net.Sockets.TcpClient : System.Net.IPEndPoint -> System.Net.Sockets.TcpClient
Public Sub New (localEP As IPEndPoint)
Parameters
- localEP
- IPEndPoint
The IPEndPoint to which you bind the TCP Socket.
Exceptions
The localEP
parameter is null
.
Examples
The following code example demonstrates how to create an instance of the TcpClient class using a local endpoint.
//Creates a TCPClient using a local end point.
IPAddress^ ipAddress = Dns::Resolve( Dns::GetHostName() )->AddressList[ 0 ];
IPEndPoint^ ipLocalEndPoint = gcnew IPEndPoint( ipAddress,11000 );
TcpClient^ tcpClientA = gcnew TcpClient( ipLocalEndPoint );
//Creates a TCPClient using a local end point.
IPAddress ipAddress = Dns.GetHostEntry (Dns.GetHostName ()).AddressList[0];
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 0);
TcpClient tcpClientA = new TcpClient (ipLocalEndPoint);
'Creates a TCPClient using a local endpoint.
Dim ipAddress As IPAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList(0)
Dim ipLocalEndPoint As New IPEndPoint(ipAddress, 0)
Dim tcpClientA As New TcpClient(ipLocalEndPoint)
Remarks
This constructor creates a new TcpClient and binds it to the IPEndPoint specified by the localEP
parameter. Before you call this constructor, you must create an IPEndPoint
using the IP address and port number from which you intend to send and receive data. You do not need to specify a local IP address and port number before connecting and communicating. If you create a TcpClient
using any other constructor, the underlying service provider will assign the most appropriate local IP address and port number.
You must call the Connect method before sending and receiving data.
Note
This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.
See also
Applies to
TcpClient(AddressFamily)
- Source:
- TCPClient.cs
- Source:
- TCPClient.cs
- Source:
- TCPClient.cs
Initializes a new instance of the TcpClient class with the specified family.
public:
TcpClient(System::Net::Sockets::AddressFamily family);
public TcpClient (System.Net.Sockets.AddressFamily family);
new System.Net.Sockets.TcpClient : System.Net.Sockets.AddressFamily -> System.Net.Sockets.TcpClient
Public Sub New (family As AddressFamily)
Parameters
- family
- AddressFamily
The AddressFamily of the IP protocol.
Exceptions
The family
parameter is not equal to AddressFamily.InterNetwork
-or-
The family
parameter is not equal to AddressFamily.InterNetworkV6
Examples
The following code example demonstrates how to create an instance of the TcpClient class.
TcpClient^ tcpClientD = gcnew TcpClient( AddressFamily::InterNetwork );
TcpClient tcpClientD = new TcpClient (AddressFamily.InterNetwork);
Dim tcpClientD As New TcpClient(AddressFamily.InterNetwork)
Remarks
Note
This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.
Applies to
TcpClient(String, Int32)
- Source:
- TCPClient.cs
- Source:
- TCPClient.cs
- Source:
- TCPClient.cs
Initializes a new instance of the TcpClient class and connects to the specified port on the specified host.
public:
TcpClient(System::String ^ hostname, int port);
public TcpClient (string hostname, int port);
new System.Net.Sockets.TcpClient : string * int -> System.Net.Sockets.TcpClient
Public Sub New (hostname As String, port As Integer)
Parameters
- hostname
- String
The DNS name of the remote host to which you intend to connect.
- port
- Int32
The port number of the remote host to which you intend to connect.
Exceptions
The hostname
parameter is null
.
An error occurred when accessing the socket.
Examples
The following code example demonstrates how to create an instance of the TcpClient class using a host name and port number.
// Creates a TCPClient using hostname and port.
TcpClient^ tcpClientB = gcnew TcpClient( "www.contoso.com",11000 );
//Creates a TCPClient using host name and port.
TcpClient tcpClientB = new TcpClient ("www.contoso.com", 11000);
'Creates a TCPClient using hostname and port.
Dim tcpClientB As New TcpClient("www.contoso.com", 11000)
Remarks
This constructor creates a new TcpClient and makes a synchronous connection attempt to the provided host name and port number. The underlying service provider will assign the most appropriate local IP address and port number. TcpClient
will block until it either connects or fails. This constructor allows you to initialize, resolve the DNS host name, and connect in one convenient step.
If IPv6 is enabled and the TcpClient(String, Int32) method is called to connect to a host that resolves to both IPv6 and IPv4 addresses, the connection to the IPv6 address will be attempted first before the IPv4 address. This may have the effect of delaying the time to establish the connection if the host is not listening on the IPv6 address.
Note
If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. After you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.
Note
This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.