5 Replies - 397 Views - Last Post: 24 October 2019 - 03:58 AM Rate Topic: -----

#1 KC10513   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 13-October 17

SslStream.EndRead returning 0 bytes

Posted 23 October 2019 - 03:30 AM

Hi All,

My server listener, works fine when talking with Ios client but when i call SslStream.BeginRead then SslStream.EndRead with a Windows client SslStream.EndRead returns 0 bytes.
I use a self-signed certificate that i created using makecert.exe to authenticate as the server
I thought the client socket was being close unintentionally but after i checked the socket is connected, can someone help me here.

 public void StartServer()
        {
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, port);
            // Create a TCP/IP socket.
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            // Bind the socket to the local endpoint and listen for incoming connections.
            try
            {                
                serverSocket.Bind(localEndPoint);
                serverSocket.Listen(100);

                while (true)
                {
                    //Set the event to nonsignaled state.
                    _mre.Reset();
                    // Start an asynchronous socket to listen for connections.
                    serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), serverSocket);
                    // Wait until a connection is made before continuing.
                    _mre.WaitOne();
                }
            }
            catch (Exception ex)
            {
                msgError("Exception: {0}", ex.Message);
            }
        }
 
  public void AcceptCallback(IAsyncResult async)
        {
            try
            {
                // Signal the main thread to continue accepting new connections
                _mre.Set();
                // Get the socket that handles the client request.
                Socket listener = (Socket)async.AsyncState;
                // Create new state object and get the client socket to work with
                StateObject state = new StateObject();
                state.workSocket = listener.EndAccept(async);
               
               ////Wrap the Networkstream inside the ssl stream for Read/Wite operation
               state.encryptedStream = new SslStream(new NetworkStream(state.workSocket, false));
               ////Start the server authentication process with an active ssl certificate
               state.encryptedStream.BeginAuthenticateAsServer(Certificate, EndAuthenticateCallBack, state);
                }
            }
            catch (Exception ex)
            {
                msgError("Exception: {0}", ex.Message);
            }
        }
        private void EndAuthenticateCallBack(IAsyncResult async)
        {
            StateObject state = (StateObject)async.AsyncState;
            try
            {
                state.encryptedStream.EndAuthenticateAsServer(async);
                if (state.encryptedStream.IsAuthenticated && state.encryptedStream.CanRead)
                {
                    Console.WriteLine("Connection secured: Read start");
                    state.encryptedStream.BeginRead(state.buffer, 0, StateObject.BufferSize, new AsyncCallback(ReceiveCallBack), state);
                }
            }
            catch (Exception ex)
            {
                msgError("Exception: {0}", ex.Message);
                if (ex.InnerException != null)
                {
                    Console.WriteLine("Inner exception: {0}", ex.InnerException.Message);
                }
                msgError("Authentication failed - closing the connection.");
                state.encryptedStream.Close();
                state.workSocket.Close();
                return;
            }
        }

 private void ReceiveCallBack(IAsyncResult async)
        {
            StateObject state = (StateObject)async.AsyncState;
            try
            {
                state = (StateObject)async.AsyncState;
               
                int bytesRead;
                bytesRead = state.encryptedStream.EndRead(async); //THIS IS RETURNING 0 BYTES

//...handling bytesRead here...
}




Is This A Good Question/Topic? 0
  • +

Replies To: SslStream.EndRead returning 0 bytes

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,248
  • Joined: 05-May 12

Re: SslStream.EndRead returning 0 bytes

Posted 23 October 2019 - 03:51 AM

How did you install the certificate into the Windows cert store? Which cert store did you install it into specifically?
Was This Post Helpful? 0
  • +
  • -

#3 KC10513   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 13-October 17

Re: SslStream.EndRead returning 0 bytes

Posted 23 October 2019 - 03:57 AM

the certificate is under the Trusted Root CA,
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,248
  • Joined: 05-May 12

Re: SslStream.EndRead returning 0 bytes

Posted 23 October 2019 - 02:39 PM

Trusted Root CA for your user, or for the entire machine?
Was This Post Helpful? 0
  • +
  • -

#5 KC10513   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 13-October 17

Re: SslStream.EndRead returning 0 bytes

Posted 23 October 2019 - 10:45 PM

for the entire machine(Local machine)
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,248
  • Joined: 05-May 12

Re: SslStream.EndRead returning 0 bytes

Posted 24 October 2019 - 03:58 AM

Yup, that is where it should be.

Have you tried disabling your AntiVirus software, particularly if it has firewall functionality?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1