Прочетете на английски Редактиране

Споделяне чрез


TcpClient Constructors

Definition

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.

C#
public TcpClient();

Examples

The following code example demonstrates how to use the parameterless constructor to create a new TcpClient.

C#
//Creates a TCPClient using the default constructor.
TcpClient tcpClientC = 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.

Бележка

On .NET Framework, this constructor works only with IPv4 address types.

Бележка

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

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

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.

C#
public TcpClient(System.Net.IPEndPoint localEP);

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.

C#
//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);

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.

Бележка

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

.NET 10 и други версии
Продукт Версии
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

TcpClient(AddressFamily)

Source:
TCPClient.cs
Source:
TCPClient.cs
Source:
TCPClient.cs

Initializes a new instance of the TcpClient class with the specified family.

C#
public TcpClient(System.Net.Sockets.AddressFamily family);

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.

C#
TcpClient tcpClientD = new TcpClient (AddressFamily.InterNetwork);

Remarks

Бележка

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

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

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.

C#
public TcpClient(string hostname, int port);

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.

The port parameter is not between MinPort and MaxPort.

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.

C#
//Creates a TCPClient using host name and port.
TcpClient tcpClientB = 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.

Бележка

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.

Бележка

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

.NET 10 и други версии
Продукт Версии
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1