473,657 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems reading network stream

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 waitForTerminat or As Boolean) As
String

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

While Not m_Stream.DataAv ailable
Threading.Threa d.Sleep(250)
End While

While m_Stream.DataAv ailable

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

If waitForTerminat or AndAlso (Not m_Stream.DataAv ailable) AndAlso
(Not sResult.EndsWit h("." & ControlChars.Cr Lf)) Then

If (m_Socket.Clien t.Poll(5000000, SelectMode.Sele ctRead) =
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
3 2460
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**@theclarkh ome.spamtin.net > escribió en el mensaje
news:eg******** ********@TK2MSF TNGP14.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 waitForTerminat or As Boolean) As
String

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

While Not m_Stream.DataAv ailable
Threading.Threa d.Sleep(250)
End While

While m_Stream.DataAv ailable

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

If waitForTerminat or AndAlso (Not m_Stream.DataAv ailable) AndAlso
(Not sResult.EndsWit h("." & ControlChars.Cr Lf)) Then

If (m_Socket.Clien t.Poll(5000000, SelectMode.Sele ctRead) =
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
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 waitForTerminat or AndAlso (Not m_Stream.DataAv ailable) AndAlso (Not sResult.EndsWit h("." & ControlChars.Cr Lf)) 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
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.goo glegroups.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 waitForTerminat or AndAlso (Not m_Stream.DataAv ailable)

AndAlso
(Not sResult.EndsWit h("." & ControlChars.Cr Lf)) 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2848
by: John | last post by:
I have a Socket open to a target system. I get the network stream from the open socket and then create a stream reader and stream writer on this stream. The stream reader and writers are operating on different threads. Does anyone know if it is possible to read and write to the same stream using two different streaming object as mentioned here. John
4
4335
by: Christopher H. Laco | last post by:
I'm having a problem with the TcpClient that I can only conclude is either a feature, or a complete misunderstanding of the docs on my part. In a nutshell, I'm simply performing the following sequence with a server: connect write(50 bytes) read(2002 bytes) write(50 bytes) read(2002 bytes)
3
9505
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At the end of this message is the inflate method this is where I get stuck I know that I need a byte array but because I am decompressing a string I have no idea of how big the byte array will need to be in the end (the inflate and deflate methods...
10
2036
by: Alejandro Castañaza | last post by:
Hi. I'm writing a program, and I need to send confidential data through the network, so I decided to use encryption, using the System.Security.Cryptography namespace. I'm using the sockets for the network communications, and the program first does a key exchange, with the asymetric cipher classes, to get a new key for the symmetric cipher. My problem is, that although I have checked that the two points get to the same key and...
21
13075
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the server is on one line (\r\n at end) and is formed as - each of which is seperated by a space. Arguments with spaces in them are enclosed in quotations. So, I'm able to open a connection to the server. When I send a message to
3
4021
by: kjell | last post by:
Hi, I'm trying to write a program that reads data from a network stream. I would like the program to read all available data in the buffer and then process the data. I do not want the program to hang unless there is no data in the buffer. For example if there are ten bytes available in the buffer I would like the program to read those ten bytes and then processed the data. If there are twenty bytes available in the buffer I would...
1
2017
by: WildBill | last post by:
I am using a tcpclient and a network stream to communicate with the TCP port on a printer for testing the printer firmware. The code I have works perfectly the first time I run it. The printer reacts correctly. If I run it again there is a long delay before the the printer reacts. When we used VB6 sockets to do this we had no problems. Below is the code I am using in Visual Studio 2005 Any help would be appreciated.
3
6764
by: Sir Psycho | last post by:
Hi, For some reason, when i step over this code, it returns the full byte stream im expecting from the server, however when I let it run with no intervention, it only seems to grab a small chunk on the stream. What am I doing wrong? There is more code than this, but this is the problem code.
8
4350
by: Peter Bradley | last post by:
Hi, I wonder if anyone can help me out? I'm trying to implement an EPP (rfc4934 and rfc4930) client. So far I've managed to connect and authorise using an X509 Certificate. This should elicit a <greetingresponse from the server, and it's the reading of this response that is giving me a bit of grief. The response shoud be
0
8826
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6166
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.