views:

51

answers:

3

I am connecting to my mail server using IMAP and Telnet. Once I am connected I am marking all items in the inbox as read. Some times the inbox will only have a couple of e-mails, sometimes the inbox may have thousands of e-mails. I am storing the response from the server into a Byte array, but the Byte array has a fixed length.

Private client As New TcpClient("owa.company.com", 143)
Private data As [Byte]()
Private stream As NetworkStream = client.GetStream()
.
. some code here generates a response that I want to read
.
data = New [Byte](1024) {}
bytes = stream.Read(data, 0, data.Length)

But the response from the server varies based on how many e-mails are successfully marked as read since I get one line of confirmation for each e-mail processed. There are times where the response may contain only 10-20 lines, other times it will contain thousands of lines. Is there any way for me to be able to get the response from the server in its entirety? I mean it seems like I would have to know when the server was done processing my request, but I'm not sure how to go about accomplishing this.

So to reiterate my question is: How can I check in my program to see when the server is done processing a response?

A: 

Why not just check the unread mail count? If there are no unread mail, then all have been marked as unread :)

Hugo
A: 

Hi there.

This article has an interesting example of C# code communicating over TCP to a server. It shows how to use a While loop to wait until the server has sent over all data over the wire.

Concentrate on the HandleClientComm() routine, since this some code you may wish to use.

Cheers. Jas.

Jason Evans
+1  A: 

I believe you can use the NetworkStream's DataAvailable property:

if( stream.CanRead)
{
  do{
     bytes = stream.Read(data, 0, data.Length);
     //append the data read to wherever you want to hold it.
     someCollectionHoldingTheFullResponse.Add( data);
  } while( stream.DataAvailable);
}

At the end, "someCollectionHoldingTheFullResponse" (memory stream? string? List<byte>? up to your requirements) would hold the full response.

Philip Rieck