2
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);
dtb
  • 213,145
  • 36
  • 401
  • 431
Roozbeh Sharafi
  • 347
  • 1
  • 6
  • 21

2 Answers2

2

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)
Jon
  • 428,835
  • 81
  • 738
  • 806
  • Thanks, your answer seems helpful but can you tell me what's wrong with my code? where's my mistake? – Roozbeh Sharafi Jun 18 '11 at 16:09
  • 1
    @RoozbehSharafi: As I already said, you should be checking the return value of `Read`. Just like the example documentation does: http://msdn.microsoft.com/en-us/library/system.net.security.sslstream.read.aspx – Jon Jun 18 '11 at 16:13
  • If you read the code more carefully, he *does* check the return value, and only echoes the number of bytes that was received. I am having a similar problem which would likely have the same solution as his problem: that every call the s.Read() returns 5 bytes of zeroes, even when the remote server has sent nothing. – Nathan Wiebe Oct 17 '19 at 14:32
  • @NathanWiebe: as written, the code doesn't check the return value. It checks the `bytes` variable, but the return value is stored in `bytes2`. In any case, the check only happens *after* he has sent the bytes onwards. In your case, either you also have some mistake, or the stream genuinely contains null bytes. – Jon Oct 18 '19 at 11:55
  • Friend, I don't know which post you are looking at. 1. The post above very clearly DOES use the return value (bytes2) - as the second to the Socket.Send function. Yes, he does have an obvious typo in the do...while condition, but he does check and use the return value. Given that his program compiles (i.e. the bytes variable must exist elsewhere), the infinite loop caused by the do...while typo in no way detracts from the point he is trying to make: that the SslStream.Read() function most certainly does return a non-zero quantity of 0 bytes (in my case 5 bytes of zero, over and over). – Nathan Wiebe Nov 08 '19 at 15:40
  • 2. Further to your hasty and incorrect diagnosis of my problem, SslStream.Read() DOES return zeroes. In my case, I pass it a buffer full of non-zero data and Read() indicates that 5 bytes have been decrypted from the stream, and the first five bytes in my buffer are zeroes after the call. I control both sides of the socket connection, and I can guarantee that no bytes have been sent by the remote host. – Nathan Wiebe Nov 08 '19 at 15:40
  • I can only assume that there is some nuance or bug in my .NET version's implementation that causes repeated Read() calls to an SslStream to return a non-zero quantity of zero bytes when the stream is connected but has no data from the remote host. In my case I switched it to async calls rather than polling the SslStream and it works perfectly. – Nathan Wiebe Nov 08 '19 at 15:41
0

bytes2 = sslStream.Read(buffer, 0, 4096); reads up to 4096 bytes into buffer, not exactly 4096 bytes. It blocks until at least one byte is read and returns the number of bytes read. So after the method call, buffer will have the same content as before the method call (e.g., filled with nulls), except for the first bytes2 bytes, which are the bytes received from the server.

dtb
  • 213,145
  • 36
  • 401
  • 431