TcpClient 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为 TCP 网络服务提供客户端连接。
public ref class TcpClient : IDisposable
public class TcpClient : IDisposable
type TcpClient = class
interface IDisposable
Public Class TcpClient
Implements IDisposable
- 继承
-
TcpClient
- 实现
示例
下面的代码示例建立 TcpClient
连接。
void Connect( String^ server, String^ message )
{
TcpClient^ client = nullptr;
try
{
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
Int32 port = 13000;
client = gcnew TcpClient(server, port);
// Translate the passed message into ASCII and store it as a Byte array.
array<Byte>^data = Text::Encoding::ASCII->GetBytes( message );
// Get a client stream for reading and writing.
NetworkStream^ stream = client->GetStream();
// Send the message to the connected TcpServer.
stream->Write( data, 0, data->Length );
Console::WriteLine( "Sent: {0}", message );
// Receive the server response.
// Buffer to store the response bytes.
data = gcnew array<Byte>(256);
// String to store the response ASCII representation.
String^ responseData = String::Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream->Read( data, 0, data->Length );
responseData = Text::Encoding::ASCII->GetString( data, 0, bytes );
Console::WriteLine( "Received: {0}", responseData );
// Explicit close is not necessary since TcpClient::Dispose() will be
// called automatically in finally block.
// stream->Close();
// client->Close();
}
catch ( ArgumentNullException^ e )
{
Console::WriteLine( "ArgumentNullException: {0}", e );
}
catch ( SocketException^ e )
{
Console::WriteLine( "SocketException: {0}", e );
}
finally
{
if (client != nullptr)
delete client;
}
Console::WriteLine( "\n Press Enter to continue..." );
Console::Read();
}
static void Connect(String server, String message)
{
try
{
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
Int32 port = 13000;
// Prefer a using declaration to ensure the instance is Disposed later.
using TcpClient client = new TcpClient(server, port);
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Get a client stream for reading and writing.
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
Console.WriteLine("Sent: {0}", message);
// Receive the server response.
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
// Explicit close is not necessary since TcpClient.Dispose() will be
// called automatically.
// stream.Close();
// client.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
Console.WriteLine("\n Press Enter to continue...");
Console.Read();
}
Shared Sub Connect(server As [String], message As [String])
Try
' Create a TcpClient.
' Note, for this client to work you need to have a TcpServer
' connected to the same address as specified by the server, port
' combination.
Dim port As Int32 = 13000
' Prefer using declaration to ensure the instance is Disposed later.
Using client As New TcpClient(server, port)
' Translate the passed message into ASCII and store it as a Byte array.
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
' Get a client stream for reading and writing.
Dim stream As NetworkStream = client.GetStream()
' Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length)
Console.WriteLine("Sent: {0}", message)
' Receive the server response.
' Buffer to store the response bytes.
data = New [Byte](256) {}
' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty
' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
Console.WriteLine("Received: {0}", responseData)
' Explicit close is not necessary since TcpClient.Dispose() will be
' called automatically.
' stream.Close()
' client.Close()
End Using
Catch e As ArgumentNullException
Console.WriteLine("ArgumentNullException: {0}", e)
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
End Try
Console.WriteLine(ControlChars.Cr + " Press Enter to continue...")
Console.Read()
End Sub
注解
类 TcpClient
提供了用于在同步阻止模式下通过网络连接、发送和接收流数据的简单方法。
为了TcpClient
连接和交换数据,TcpListener使用 TCP ProtocolType 创建的 或 Socket 必须侦听传入的连接请求。 可以通过以下两种方式之一连接到此侦听器:
Create 并调用三种
TcpClient
可用Connect方法之一。使用远程主机的主机名和端口号Create
TcpClient
。 此构造函数将自动尝试连接。
注意
如果要在同步阻止模式下发送无连接数据报,请使用 UdpClient 类。
继承者说明
若要发送和接收数据,请使用 GetStream() 方法获取 NetworkStream。 Write(Byte[], Int32, Int32)调用 的 NetworkStream 和 Read(Byte[], Int32, Int32) 方法以使用远程主机发送和接收数据。 Close(Int32)使用 方法释放与 TcpClient关联的所有资源。
构造函数
TcpClient() |
初始化 TcpClient 类的新实例。 |
TcpClient(AddressFamily) |
使用指定的族初始化 TcpClient 类的新实例。 |
TcpClient(IPEndPoint) |
初始化 TcpClient 类的新实例,并将其绑定到指定的本地终结点。 |
TcpClient(String, Int32) |
初始化 TcpClient 类的新实例并连接到指定主机上的指定端口。 |
属性
Active |
获取或设置一个值,它指示是否已建立连接。 |
Available |
获取已经从网络接收且可供读取的数据量。 |
Client |
获取或设置基础 Socket。 |
Connected | |
ExclusiveAddressUse | |
LingerState |
获取或设置有关关联的套接字的延迟状态的信息。 |
NoDelay |
获取或设置一个值,该值在发送或接收缓冲区未满时禁用延迟。 |
ReceiveBufferSize |
获取或设置接收缓冲区的大小。 |
ReceiveTimeout |
获取或设置在初始化一个读取操作以后 TcpClient 等待接收数据的时间量。 |
SendBufferSize |
获取或设置发送缓冲区的大小。 |
SendTimeout |
获取或设置 TcpClient 等待发送操作成功完成的时间量。 |
方法
显式接口实现
IDisposable.Dispose() |
此 API 支持产品基础结构,不能在代码中直接使用。 释放由 TcpClient 使用的所有资源。 |