I would like to write a client and server application that uses a tcp socket for communication. However, I do not need a certificate, because users do not care who the other is. But I need asymmetric encryption, so no one can track. I can't think of a way to exchange symmetric keys without using RSA, but in any case, I think I should use SSL with TCP?
What is the maximum way to create an SSL / TCP connection in C #? I believe that I either do not specify cert, or dynamically create one of the flies (but they should be thrown ...). What is the easiest way to implement it? I tried just adding sslstream to the tcp stream, but it doesnβt cut it (I get streams that are not readable errors). So far my simple code
{ Thread serverThread = new Thread((ThreadStart)delegate { TcpListener listener = new TcpListener(1234); listener.Start(); using (TcpClient client = listener.AcceptTcpClient()) //using (var sslstream = client.GetStream()) using (var sslstream = new System.Net.Security.SslStream(client.GetStream(), true)) using (StreamReader reader = new StreamReader(sslstream)) using (StreamWriter writer = new StreamWriter(sslstream)) { writer.AutoFlush = true; string inputLine; while ((inputLine = reader.ReadLine()) != null) { writer.WriteLine(inputLine); } } }); serverThread.Start(); { string[] linesToSend = new string[] { "foo", "bar", "ack" }; using (TcpClient client = new TcpClient("127.0.0.1", 1234)) //using (var sslstream = client.GetStream()) using (var sslstream = new System.Net.Security.SslStream(client.GetStream(), true)) using (StreamReader reader = new StreamReader(sslstream)) using (StreamWriter writer = new StreamWriter(sslstream)) { writer.AutoFlush = true; foreach (string lineToSend in linesToSend) { Console.WriteLine("Sending to server: {0}", lineToSend); writer.WriteLine(lineToSend); string lineWeRead = reader.ReadLine(); Console.WriteLine("Received from server: {0}", lineWeRead); //Thread.Sleep(2000); // just for effect } Console.WriteLine("Client is disconnecting from server"); } } serverThread.Join(); }
user34537
source share