using System; using System.Net; using System.Net.Sockets; class Program { static void Main(string[] args) { try { // Create a TcpClient. TcpClient client = new TcpClient(); // Connect to the remote endpoint. client.Connect(IPAddress.Parse("127.0.0.1"), 8000); // Do some work here. // Close the connection. client.Close(); } catch (Exception e) { Console.WriteLine("Exception: {0}", e); } } }
using System; using System.Net.Sockets; class Program { static void Main(string[] args) { try { // Create a TcpClient. TcpClient client = new TcpClient("127.0.0.1", 8000); // Get a stream object for writing and reading. NetworkStream stream = client.GetStream(); // Send a message to the server. byte[] message = System.Text.Encoding.ASCII.GetBytes("Hello, server!"); stream.Write(message, 0, message.Length); // Receive the server's response. byte[] buffer = new byte[1024]; int bytesRead = stream.Read(buffer, 0, buffer.Length); string response = System.Text.Encoding.ASCII.GetString(buffer, 0, bytesRead); Console.WriteLine("Received: {0}", response); // Close the connection. client.Close(); } catch (Exception e) { Console.WriteLine("Exception: {0}", e); } } }This code is using the .NET framework package library.