Note that there are some explanatory texts on larger screens.

plurals
  1. PONetworkStream.DataAvailable property does not return the right result when using SslStream
    primarykey
    data
    text
    <p>I've got an application with a persistent socket (it's open when the application starts and closed along with the application).</p> <p>This socket is used by a server to push some data.</p> <p>Since this connection could be either HTTP or HTTPS, I wrote this code to initialize my objects:</p> <pre><code>s_tcpClient = new TcpClient(s_server.CometIp, s_server.CometPort); s_tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, false); s_tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, CST_STREAM_BUFFER_SIZE); s_tcpClient.ReceiveTimeout = 0; s_networkStream = s_tcpClient.GetStream(); s_cometStreamReader = null; if (s_server.Protocol == "HTTPS") { SslStream sslStream = new SslStream( s_tcpClient.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate) ); sslStream.AuthenticateAsClient(s_server.CometIp); s_cometStreamReader = new StreamReader(sslStream, Encoding.UTF8, true, CST_STREAM_BUFFER_SIZE); s_cometStream = sslStream as Stream; } else { s_cometStreamReader = new StreamReader(s_networkStream, Encoding.UTF8, true, CST_STREAM_BUFFER_SIZE); s_cometStream = s_networkStream as Stream; } </code></pre> <p>This way, I can handle both HTTP and HTTPS protocols.</p> <p>Then, I've got this loop (in a thread) which waits for data to be available in the socket:</p> <pre><code>while (s_networkStream.DataAvailable) { numberOfBytesRead = s_cometStream.Read(buffer, 0, buffer.Length); message.Append(Encoding.UTF8.GetString(buffer, 0, numberOfBytesRead)); ExtractMessages(ref message); } </code></pre> <p>Here are my 2 questions:</p> <ol> <li><p>The documentation says that the Read method returns 0 if no data is available. But in my case, Read either returns a number > 0 (when some data have been read) or throws an exception because it timed out... What am I missing, here? Under which circumstances does Read return 0?</p></li> <li><p>When my connection uses HTTP protocol, this code works fine. I receive everything and if a message is bigger than my buffer size (CST_STREAM_BUFFER_SIZE), DataAvailable remains true, and the loop reads all remaining data. But when I try this with HTTPS, it works only for messages smaller than my buffer. If a message if bigger, the method Read first reads CST_STREAM_BUFFER_SIZE bytes and then DataAvailable becomes false, although some bytes are obviously ready to be read. And in this case, receiving another message "unstucks" the remaining data because DataAvailable becomes true again. What am I doing wrong? Why does not DataAvailable act the same in both cases?</p></li> </ol> <p>Any help would be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload