Problem with asynchronous NetworkStream - BeginRead / EndRead

Nov 27 2005 7:30 PM
I am reading data from a network stream between this application and an SMTP server. I am expecting a initial message from the SMTP serveer someting like "220 stmp.myserver.com ESMTP". The bytes received by the EndRead method seem to be about right for the expected message, although no data is being written into my readBuffer byte array.

Below is the relevant portion of my code and testing output.

// BEGIN CODE

private void BeginReadSmtp()
{
byte[] readBuffer = new byte[1024];
smtpStream.BeginRead(readBuffer, 0, readBuffer.Length, new AsyncCallback(EndReadSmtp),smtpStream);
}

void EndReadSmtp(IAsyncResult iar)
{
smtpStream = (NetworkStream)iar.AsyncState;
byte[] readBuffer = new byte[1024];
String data = "";
int bytesRead;

bytesRead = smtpStream.EndRead(iar);
data = String.Concat(data, Encoding.ASCII.GetString(readBuffer, 0, bytesRead));

while (smtpStream.DataAvailable)
{
smtpStream.BeginRead(readBuffer, 0, readBuffer.Length, new AsyncCallback(EndReadSmtp),smtpStream);
}
Console.WriteLine("BYTES RECEIVED:{0}",bytesRead);
Console.WriteLine("DATA:{0}",data);

}

// END CODE


PROGRAM OUTPUT IS AS FOLLOWS:

BYTES RECEIVED: 72
DATA:


Any suggestions?
Thanks.