// 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.Security.Cryptography.X509Certificates; using System.IO; using System.Net.Security; using System.Net.Sockets; using System.Security.Authentication; namespace SocketDemo.Text.SecureHigh.Client { public class Program { private static bool CertCheck(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { Console.WriteLine("(server is {0})", certificate.Subject); return true; } 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(); SslStream sslstm = new SslStream(stm, false, CertCheck); sslstm.AuthenticateAsClient("CN=Arne Vajhoej,OU=Demo,O=Personal,L=Coventry,ST=RI,C=US", null, SslProtocols.Tls12, true); StreamReader sr = new StreamReader(sslstm); StreamWriter sw = new StreamWriter(sslstm); 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(); } } }