TcpClient Class
Provides client connections for TCP network services.
Assembly: System (in System.dll)
The TcpClient class provides simple methods for connecting, sending, and receiving stream data over a network in synchronous blocking mode.
In order for TcpClient to connect and exchange data, a TcpListener or Socket created with the TCP ProtocolType must be listening for incoming connection requests. You can connect to this listener in one of the following two ways:
Create a TcpClient and call one of the three available Connect methods.
Create a TcpClient using the host name and port number of the remote host. This constructor will automatically attempt a connection.
Note: |
|---|
If you want to send connectionless datagrams in synchronous blocking mode, use the UdpClient class. |
To send and receive data, use the GetStream method to obtain a NetworkStream. Call the Write and Read methods of the NetworkStream to send and receive data with the remote host. Use the Close method to release all resources associated with the TcpClient.
The following code example establishes a TcpClient connection.
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 'Connect
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[] = 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(S"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 = Text::Encoding::ASCII->GetString(data, 0, bytes);
Console::WriteLine(S"Received: {0}", responseData);
// Close everything.
client->Close();
} catch (ArgumentNullException* e) {
Console::WriteLine(S"ArgumentNullException: {0}", e);
} catch (SocketException* e) {
Console::WriteLine(S"SocketException: {0}", e);
}
Console::WriteLine(S"\n Press Enter to continue...");
Console::Read();
}
- SocketPermission
Permission to establish an outgoing connection or accept an incoming request.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note: