Search code examples
c#tcpclientsslstream

sslStream.Read problem: all bytes read are 0


TcpClient client = new TcpClient("69.147.112.160", 443);
SslStream sslStream = new SslStream(client.GetStream(),false,
                                    ValidateServerCertificate,null);
try
{
    sslStream.AuthenticateAsClient("mail.yahoo.com");
}
catch (AuthenticationException e)
{

    return;
}
byte[] messsage = Encoding.UTF8.GetBytes(".<EOF>");
sslStream.Write(messsage);
sslStream.Flush();
byte[] buffer = new byte[4096];
int bytes2 = -1;
do
{
    /**************************************************
     *** JUST A LINE BELOW ALL buffer BYTES ARE ZERO!**
     *************************************************/

    bytes2 = sslStream.Read(buffer, 0, 4096);
    m_sockClient.Send(buffer, bytes2, 0);
} while (bytes != 0);

Solution

  • All bytes in buffer that have not been filled in by the Read call will be zero; this is standard C#.

    If every last one byte in there is zero, only two things can be responsible:

    • You read real null bytes from the stream (unlikely)
    • Read does not read anything (in which case it returns 0 -- you should definitely be checking the return value)