Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8194897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:57:45+00:00 2026-06-07T04:57:45+00:00

I would like to write a client and server app which uses a tcp

  • 0

I would like to write a client and server app which uses a tcp socket to communicate. However I do not need a cert as the users don’t care who the other is. But i do need asymmetrical encryption so no one can snoop. I can’t think of a way to share the symmetrical keys w/o using RSA but anyways i think i should be using SSL with TCP?

What is the most way to create an SSL/TCP connection in C#? I imagine i either specify no cert or dynamically create one of the fly (but they should be throwaway…). What is the most simple way to implement it? I tried simply adding a sslstream to my tcp stream but that doesnt cut it (i get stream not readable errors). Heres my simple code so far

    {
        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();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:57:47+00:00Added an answer on June 7, 2026 at 4:57 am

    You need to provide an implementation for LocalCertificateSelectionCallback to pick the certificate to use and you need to provide an implementation for RemoteCertificateValidationCallback to accept the self-signed certificate used by the peer.

    To create a self-signed certificate (ie. throw-away) see Creating a self-signed certificate in C#.

    There is also a step-by-step example here on SO at Using SSL and SslStream for peer to peer authentication.

    Make sure you comprehend the implications, ie. a man-in-the-middle can ear drop your traffic as you do not actually validate the certificate presented. In other words this is not at all a form of authentication (as suggested in the SO linked answer) but just a way to obtain a private communication with somebody, but you don’t really know with whom exactly. An alternative would be to use a root certificate to sign each throw away certificate used and validate the certificate chain in the client, but that would complicate significantly the generation of the cert compared with the blog link.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to write an HTTP server that answer to request using a
I'm writing my first client/server android app, and need an advice regarding server architecture.
My client/server application is using WCF for communication, which has been great. However one
I need to write a client-server application. I want to write it in python,
I would like to write an app on Android to upload my GPS location
I would like to write an abstract base class class func_double_double_t : public unary_function<double,
I would like to write a function that creates a plot of a set
I would like to write an if statement that would continue to repeat a
I would like to write a query that returns a single date value, that
I would like to write a League Fixture generator in python, but I can't.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.