swolff1978
swolff1978

Reputation: 1865

How can my program know when the server is done processing my request?

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?

Upvotes: 1

Views: 140

Answers (3)

Jason Evans
Jason Evans

Reputation: 29186

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.

Upvotes: 0

Philip Rieck
Philip Rieck

Reputation: 32568

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.

Upvotes: 1

WhyNotHugo
WhyNotHugo

Reputation: 9924

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

Upvotes: 0

Related Questions