Problems reading network stream

A

Alex Clark

Hi All,

I'm having some problems reading a network stream. I'm writing a
lightweight POP client to handle a very specific task, but I keep
unexpectedly reaching the end of the datastream when downloading a large-ish
email (about 10K). This happens about 50% of the time when trying to
download a large email from my POP server. I've tried telnet'ing into the
POP server and manually issuing commands, and it works perfectly --- the
entire email is spat out at my telnet client almost instantly every time.

I have a routine (posted below) which loops through the data in the
datastream, appending it to a string. It waits until it receives the
terminator, a "." followed by a CrLf. Unfortunately, it seems to be
"running out of steam" before the end of the message is downloaded.

I'd really appreciate any help anyone can offer on this as it seems very odd
to me that it would work ok about half of the time and then just completely
time out the other half!

Thanks in advance,
Alex Clark



The code in the routine is as follows:

-------------------------------------------------------
Private Function WaitForResponse(ByVal waitForTerminator As Boolean) As
String

Dim iBR, iTBR As Integer, bBuff(m_Socket.ReceiveBufferSize) As Byte
Dim sResult As String = String.Empty

While Not m_Stream.DataAvailable
Threading.Thread.Sleep(250)
End While

While m_Stream.DataAvailable

iBR = m_Stream.Read(bBuff, 0, bBuff.Length)
iTBR += iBR
sResult &= GetString(bBuff)

If waitForTerminator AndAlso (Not m_Stream.DataAvailable) AndAlso
(Not sResult.EndsWith("." & ControlChars.CrLf)) Then

If (m_Socket.Client.Poll(5000000, SelectMode.SelectRead) =
False) Then
Stop ' Timed out waiting for the end of the message
End If

End If

End While

Return sResult

End Function

-------------------------------------------------------
 
C

Carlos J. Quintero [.NET MVP]

Hi Alex,

I don´t know the specific answer to your question because I am still
learning about this too, but I bought a book "C# Network Programming" by
Richard Blum which has several chapters explaining how to code properly
these things and how to solve "When TCP Goes Bad" situations.

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com
 
R

rich_blum

Alex said:
I have a routine (posted below) which loops through the data in the
datastream, appending it to a string. It waits until it receives the
terminator, a "." followed by a CrLf. Unfortunately, it seems to be
"running out of steam" before the end of the message is downloaded.

If waitForTerminator AndAlso (Not m_Stream.DataAvailable) AndAlso
(Not sResult.EndsWith("." & ControlChars.CrLf)) Then

Alex -

Your logic will stop reading data from the stream as soon as the
received buffer happens to end with a ".CrLf". This can happen any time
a buffer ends with a complete sentence, without it being the end of the
message.

In POP, you know you are at the end of the message when you see
"CrLf.CrLf" (a period on a line by itself). You should check for that
condition instead.

Alternatively, this is an excellent time to use the ReadLine()
method in the StreamReader class. Just keep reading lines until you get
one with only a period in it.

Hope this helps solve your problem.

Rich Blum - author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
 
A

Alex Clark

Thanks Rich,

It turns out it was a problem with the way I was decoding the byte-stream
into a string, if the entire buffer wasn't filled (which was often the case
for the last read of the stream) then it ended with CrLf.CrLf followed by
loads of Null chars, which were messing things up.

However, thanks for pointing out that other issue, you're quite right that
it could've caused the email to end unexpectedly if the end of a sentence
occurred at the end of one of the initial stream-reads. Thanks for that!

Kind Regards,
Alex Clark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top