添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
public ref class TcpClient : IDisposable
public class TcpClient : IDisposable
type TcpClient = class
    interface IDisposable
Public Class TcpClient
Implements IDisposable
TcpClient // 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) // 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]) ' 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 类。

  •