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 )
{
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;
TcpClient^ 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.
// Stream stream = client->GetStream();
NetworkStream^ stream = client->GetStream();
// Send the message to the connected TcpServer.
stream->Write( data, 0, data->Length );
Console::WriteLine( "Sent: {0}", message );
// Receive the TcpServer::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 );
// Close everything.
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();
}
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;
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.
// Stream stream = client.GetStream();
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
Console.WriteLine("Sent: {0}", message);
// Receive the TcpServer.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);
// Close everything.
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
Dim 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.
' Stream stream = client.GetStream();
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 TcpServer.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)
' Close everything.
stream.Close()
client.Close()
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 或 Socket 使用 TCP ProtocolType 创建的数据必须侦听传入连接请求。 可以通过以下两种方式之一连接到此侦听器:
TcpClient
创建并调用三种可用Connect方法之一。使用远程主机的主机名和端口号创建一个
TcpClient
。 此构造函数将自动尝试连接。
备注
如果要在同步阻塞模式下发送无连接数据报,请使用 UdpClient 类。
继承者说明
若要发送和接收数据,请使用GetStream()该方法获取 。NetworkStream Write(Byte[], Int32, Int32) Read(Byte[], Int32, Int32)调用远程主机发送和接收数据的方法和方法NetworkStream。 Use the Close(Int32) method to release all resources associated with the 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 使用的所有资源。 |