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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:44:40+00:00 2026-05-27T22:44:40+00:00

I am using an SslStream to encrypt a TCP connection between a client and

  • 0

I am using an SslStream to encrypt a TCP connection between a client and server. The problem is that when the client reads the data, it may be given a bunch of zero bytes instead of the real data. Here is an example that shows the issue:

        // Server
        using (NetworkStream tcpStream = client.GetStream())
        {
            Stream stream = tcpStream;
            if (ssl)
            {
                SslStream sslStream = new SslStream(tcpStream, true);
                sslStream.AuthenticateAsServer(cert, false, SslProtocols.Default, false);
                stream = sslStream;
            }

            byte[] buf = new byte[] {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02};
            stream.Write(buf, 0, buf.Length);

            buf = new byte[] {0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03};
            stream.Write(buf, 0, buf.Length);
        }



        // Client
        using (NetworkStream tcpStream = client.GetStream())
        {
            Stream stream = tcpStream;
            if (ssl)
            {
                SslStream sslStream = new SslStream(
                    tcpStream, 
                    true, 
                    delegate { return true; }
                );
                sslStream.AuthenticateAsClient(
                    "localhost",
                    null,
                    SslProtocols.Default,
                    false
                    );
                stream = sslStream;
            }

            byte[] buf = new byte[7];
            stream.Read(buf, 0, buf.Length);
            // buf is 01010101010101 as expected

            buf = new byte[9];
            stream.Read(buf, 0, buf.Length);
            // buf is 020000000000000000 instead of the expected 020303030303030303
            // a subsequent read of 8 bytes will get me 0303030303030303
            // if the ssl bool is set to false, then the expected data is received without the need for a third read
        }

It appears as though the client needs to read from the stream in the exact same number of bytes as the server wrote them only when an SslStream is being used. This can’t be right. What am I missing here?

  • 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-27T22:44:40+00:00Added an answer on May 27, 2026 at 10:44 pm

    This code

    buf = new byte[9];
    stream.Read(buf, 0, buf.Length);
    

    requests stream to read between 1 and 9 bytes into buf. It does not always read exactly 9 bytes.

    The Read Method returns the number of bytes actually read.

    Try this:

    byte[] buffer = new byte[9];
    int offset = 0;
    int count = buffer.Length;
    
    do
    {
        int bytesRead = stream.Read(buffer, offset, count);
        if (bytesRead == 0)
            break; // end of stream
        offset += bytesRead;
        count -= bytesRead;
    }
    while (count > 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to provide secure communication between various processes that are using TCP/IP sockets
Using ASP.NET MVC there are situations (such as form submission) that may require a
I have small TcpClient app to connect to a server using SSL (SSlStream class)
How to send\recive SslStream from TCP server in C#? I need a simple tutorial
I am trying to send info using USLStream from flash client to JAVA server.
I'm trying to securely transfer files between 2 devices, so I'm using an SslStream
I am working on a .Net server application that uses SslStream to provide its
I have written a TCP server using the Socket class's asynchronous/IOCP methods, BeginSend()/BeginRead()/etc. I
We have a client/server system where all communications are done using a native protocol
There is a client/server application written using Boost's ASIO (Boost v.1.48) + OpenSSL (v.1.0.0d).

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.