// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Threading; namespace E { public class MainClass { private const int PORT = 12345; private static TextWriter savout; private static TextReader savin; private static void StartPipeOut() { TcpListener list = new TcpListener(IPAddress.Any, PORT); list.Start(); TcpClient cli = list.AcceptTcpClient(); TextWriter sw = new StreamWriter(cli.GetStream()); savout = Console.Out; Console.SetOut(sw); } private static void EndPipeOut() { Console.Out.Close(); Console.SetOut(savout); } private static void StartPipeIn() { Thread.Sleep(0); TcpClient cli = new TcpClient("localhost", PORT); TextReader sr = new StreamReader(cli.GetStream()); savin = Console.In; Console.SetIn(sr); } private static void EndPipeIn() { Console.In.Close(); Console.SetIn(savin); } public static void First() { StartPipeOut(); int n = 0; for(int i = 0; i < 100000; i++) { Console.WriteLine("This is a test"); n++; } EndPipeOut(); Console.WriteLine("Wrote " + n); } public static void Second() { StartPipeIn(); int n = 0; string line; while((line = Console.ReadLine()) != null) { n++; } EndPipeIn(); Console.WriteLine("Read " + n); } public static void Main(string[] args) { Thread first = new Thread(new ThreadStart(First)); first.Start(); Thread second = new Thread(new ThreadStart(Second)); second.Start(); first.Join(); second.Join(); Console.ReadLine(); } } }