// 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.Sockets; namespace Demo { /// /// Class for simple socket communication - one request and one response. /// public class SocketComm { /// /// Send request and receive response. /// /// host name /// port number /// request /// response public static string SendAndReceivce(string host, int port, string request) { TcpClient client = new TcpClient(host, port); StreamWriter writer = new StreamWriter(client.GetStream()); StreamReader reader = new StreamReader(client.GetStream()); writer.Write(request); writer.Flush(); // must flush or method will hang string result = reader.ReadToEnd(); reader.Close(); writer.Close(); client.Close(); return result; } } public class Program { public static void Main(string[] args) { Console.WriteLine(SocketComm.SendAndReceivce("localhost", 81, "GET / HTTP/1.0\r\n\r\n")); } } }