// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.IO; using System.Net.Sockets; namespace SocketDemo.Text.High.Client { public class Program { private const string HOST = "localhost"; private const int PORT = 12345; public static void Main(string[] args) { // open connection TcpClient cli = new TcpClient(HOST, PORT); // get reader and writer NetworkStream stm = cli.GetStream(); StreamReader sr = new StreamReader(stm); StreamWriter sw = new StreamWriter(stm); Console.WriteLine("(connected)"); // send a and b int a = 123; int b = 456; sw.WriteLine(a + " " + b); sw.Flush(); // receive c and print int c = int.Parse(sr.ReadLine()); Console.WriteLine(c); // send v string v = "ABC"; sw.WriteLine(v); sw.Flush(); // receive v2 and print string v2 = sr.ReadLine(); Console.WriteLine(v2); // close connection sr.Close(); sw.Close(); cli.Close(); Console.ReadKey(); } } }