By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,033 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Problems reading network stream

P: n/a
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

-------------------------------------------------------

Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
"Alex Clark" <al**@theclarkhome.spamtin.net> escribió en el mensaje
news:eg****************@TK2MSFTNGP14.phx.gbl...
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

-------------------------------------------------------

Nov 21 '05 #2

P: n/a
Alex Clark wrote:

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

Nov 21 '05 #3

P: n/a
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


<ri*******@juno.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Alex Clark wrote:

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

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.