TcpClient Class
Provides client connections for TCP network services.
For a list of all members of this type, see TcpClient Members.
System.Object
System.Net.Sockets.TcpClient
[Visual Basic] Public Class TcpClient Implements IDisposable [C#] public class TcpClient : IDisposable [C++] public __gc class TcpClient : public IDisposable [JScript] public class TcpClient implements IDisposable
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
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.
Notes to Inheritors: 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.
Example
[Visual Basic, C#, C++] The following example establishes a TcpClient connection.
[Visual Basic] 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. 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 [C#] 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. 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(); } [C++] 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(); }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Net.Sockets
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
Assembly: System (in System.dll)
.NET Framework Security:
- SocketPermission to establish an outgoing connection or accept an incoming request.
See Also
TcpClient Members | System.Net.Sockets Namespace | TcpListener | NetworkStream | TCP/UDP | Socket | ProtocolType | IPEndPoint | Connect | Write | Read