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 6121241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:45:43+00:00 2026-05-23T15:45:43+00:00

I’m creating an application that requires an encrypted connection, and am having problems. I

  • 0

I’m creating an application that requires an encrypted connection, and am having problems. I am running both the client and server from the same computer. If I connect without SSL (via TCPClient and TCPListener, everything works perfectly fine. However, using the SSLStream class keeps giving me SocketException 0x80004005 “Connection Actively Refused”. I’ve been looking around at other posts and I can’t seem to pinpoint my problem.

Client:

private void okButton_Click(object sender, RoutedEventArgs e)
    {

        string serverCertName = COMPUTER_NAME;
        string machineName = "127.0.0.1";

        SslStream stream = runClient(machineName, serverCertName, int.Parse(clientPort.Text));

        if (stream.CanWrite)
        {
            MessageBox.Show("We can write to the stream. We have a connection!");
        }
        splitFile(this.sourceName);
    }

    public static bool ValidateServerCertificate(
        object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        if (sslPolicyErrors == SslPolicyErrors.None)
        {
            return true;
        }
        //disallow communication with unauthenticated servers
        return false;
    }

    public static SslStream runClient(string machineName, string serverName, int port)
    {
        //create client socket, machineName is the host running the server
        //THIS IS WHERE EVERYTHING FAILS
        TcpClient client = new TcpClient(machineName, port);
        SslStream sslStream = new SslStream(client.GetStream(),
            false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);

        //server should match that is on that server cert
        try
        {
            sslStream.AuthenticateAsClient(serverName);
        }
        catch (AuthenticationException e)
        {
            if (e.InnerException != null)
            {

            }
            client.Close();
            return null;
        }

        //encode message as byte array for testing, etc
        return sslStream;
    }

Server:

static X509Certificate serverCertificate = null;
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        IPAddress ip = IPAddress.Parse("127.0.0.1");
        int port = 13000;
        string certificate = COMPUTER_NAME;

        runServer(ip, port, certificate);
    }

    /// Start the ssl tcp listener
    public static void runServer(IPAddress ip, int port, string certificate)
    {
        serverCertificate = X509Certificate.CreateFromCertFile(certificate);
        TcpListener listener = new TcpListener(IPAddress.Any, port);
        listener.Start();

        while (true)
        {
            TcpClient client = listener.AcceptTcpClient();
            processClient(client);
        }
    }

    //executes if a client has connected. Create the SslStream using the connected client's stream
    static void processClient(TcpClient client)
    {
        SslStream sslStream = new SslStream(client.GetStream(), false);

        //authenticate the server, but doesn't require the client to authenticate
        try
        {
            sslStream.AuthenticateAsServer(serverCertificate, false, SslProtocols.Tls, true);
            //set timeouts to 5 seconds. Seems like a long time. If nothing happens after 5 sec it won't happen
            sslStream.ReadTimeout = 5000;
            sslStream.WriteTimeout = 5000;
            string data = readMessage(sslStream);
        }
        catch (AuthenticationException e)
        {
            Console.WriteLine("Exception: {0}", e.Message);
            if (e.InnerException != null)
            {
                Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
            }
            Console.WriteLine("Authentication failed - closing the connection.");
            sslStream.Close();
            client.Close();
            return;
        }
        finally
        {
            sslStream.Close();
            client.Close();
        }
    }

I’m brand new to TCP/IP connections in general in C#, so it’s entirely possible I did something silly, wrong, or overly complicated. I had found (via stack overflow) that SslStream was the way to go, but I just can’t seem to make it work. Does anybody have any ideas? I’d really appreciate the help.

Thank you much in advance!

  • 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-05-23T15:45:44+00:00Added an answer on May 23, 2026 at 3:45 pm

    I think that you are trying to authenticate as a client, but your server is not authenticating as a server. Try and make sure that this is happening at the same time and it should work.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string

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.