473,795 Members | 3,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

GetStream().Rea d problem


Hi,

I have an iteration to retrieve a number of messages from a server. Within this iteration, I am using the following code:

do
{
readBytes = base.GetStream( ).Read(received , 0, received.Length );
string textToAdd = Encoding.ASCII. GetString(recei ved, 0, readBytes);
myCompleteMessa ge = String.Concat(m yCompleteMessag e, textToAdd);
}
while(base.GetS tream().DataAva ilable == true);

The problem that I have seen is that not the entire content of the message is retrieved. For example, I first read the header and then the body, and when I read the second message, the header still contains some information that belonged to the previous message's body.

Any idea?

Thanks
Mike
Nov 16 '05 #1
7 6441
Mike <no****@nospam. com> wrote:
I have an iteration to retrieve a number of messages from a server.
Within this iteration, I am using the following code:

do
{
readBytes = base.GetStream( ).Read(received , 0, received.Length );
string textToAdd = Encoding.ASCII. GetString(recei ved, 0, readBytes);
myCompleteMessa ge = String.Concat(m yCompleteMessag e, textToAdd);
}
while(base.GetS tream().DataAva ilable == true);

The problem that I have seen is that not the entire content of the
message is retrieved. For example, I first read the header and then
the body, and when I read the second message, the header still
contains some information that belonged to the previous message's
body.


The problem is your use of DataAvailable, which only says whether or
not there's any data available right now. You should use something else
in the protocol to say how long messages are, or specify the message
end, etc.

You should also use a StringBuilder rather than calling String.Concat
repeatedly, as currently you're creating loads of extra strings for no
reason.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Hi Mike,
The thing is is that you are reading raw data, there is no concept of "message" as a logic data organization. either you improve your protocol to include a way to indicate the end of message ; for example SMTP use a line with a single dot "." to indicate the end of the email being sent;

or if you don;t have control over the protocol you can read the raw data and then later do the processing of it.
Also as Jon suggested you should not use String but StringBuilder.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Mike" <no****@nospam. com> wrote in message news:Ou******** ******@tk2msftn gp13.phx.gbl...

Hi,

I have an iteration to retrieve a number of messages from a server. Within this iteration, I am using the following code:

do
{
readBytes = base.GetStream( ).Read(received , 0, received.Length );
string textToAdd = Encoding.ASCII. GetString(recei ved, 0, readBytes);
myCompleteMessa ge = String.Concat(m yCompleteMessag e, textToAdd);
}
while(base.GetS tream().DataAva ilable == true);

The problem that I have seen is that not the entire content of the message is retrieved. For example, I first read the header and then the body, and when I read the second message, the header still contains some information that belonged to the previous message's body.

Any idea?

Thanks
Mike
Nov 16 '05 #3

Hi Ignacio,

Thanks for your answer. I am actually coding against a NNTP server, but the current code I have is slow retrieving messages, so I am trying to find another solution. I have tried this other code:

1: while (true)
2: {
3: byte[] received = new byte[base.ReceiveBuf ferSize];
4: stream.Read(rec eived, 0, received.Length );
5: b.Append(Encodi ng.ASCII.GetStr ing(received));
6: }

but when the iteration goes through line 4 the second time and the server only sent a single line, the application hangs. I could not find a way to stop the iteration when there is no data or all data has been read.

What am I doing wrong here?

Thanks
Mike


"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote in message news:Ot******** ******@TK2MSFTN GP12.phx.gbl...
Hi Mike,
The thing is is that you are reading raw data, there is no concept of "message" as a logic data organization. either you improve your protocol to include a way to indicate the end of message ; for example SMTP use a line with a single dot "." to indicate the end of the email being sent; or if you don;t have control over the protocol you can read the raw data and then later do the processing of it.
Also as Jon suggested you should not use String but StringBuilder.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Mike" <no****@nospam. com> wrote in message news:Ou******** ******@tk2msftn gp13.phx.gbl...

Hi,

I have an iteration to retrieve a number of messages from a server. Within this iteration, I am using the following code:

do
{
readBytes = base.GetStream( ).Read(received , 0, received.Length );
string textToAdd = Encoding.ASCII. GetString(recei ved, 0, readBytes);
myCompleteMessa ge = String.Concat(m yCompleteMessag e, textToAdd);
}
while(base.GetS tream().DataAva ilable == true);

The problem that I have seen is that not the entire content of the message is retrieved. For example, I first read the header and then the body, and when I read the second message, the header still contains some information that belonged to the previous message's body.

Any idea?

Thanks
Mike
Nov 16 '05 #4
Mike <no****@nospam. com> wrote:
Thanks for your answer. I am actually coding against a NNTP server,
but the current code I have is slow retrieving messages, so I am
trying to find another solution. I have tried this other code:

1: while (true)
2: {
3: byte[] received = new byte[base.ReceiveBuf ferSize];
4: stream.Read(rec eived, 0, received.Length );
5: b.Append(Encodi ng.ASCII.GetStr ing(received));
6: }

but when the iteration goes through line 4 the second time and the
server only sent a single line, the application hangs. I could not
find a way to stop the iteration when there is no data or all data
has been read.

What am I doing wrong here?


The client can never know when the server's going to send some more
data unless the connection has been closed. The above will actually
continue for ever, and normally you'd check whether Read returned 0
(for "connection closed"). That leads me to another problem you've got
in the code above - you're always *assuming* that Read will fill the
whole of your buffer. Your code *ought* to be more like:

byte[] buffer = new byte[base.ReceiveBuf ferSize];
while (true)
{
int read = stream.Read(buf fer, 0, buffer.Length);
if (read==0)
{
break;
}
b.Append (Encoding.ASCII .GetString(rece ived, 0, read));
}

That still won't solve your problem though, because it will hang while
the NNTP server waits for some more input from you (at least, it'll
hang until the server decides to time your connection out).

Depending on what command you're using, the response will be terminated
either at the end of a line or (IIRC) with a "." on a line on its own -
consult the relevant RFC for details. When you've found that
terminator, *that's* the point at which to take whatever your next
action is. Note that (possibly not for NNTP, but for other protocols)
you may end up reading into the next message or bit of response data,
in which case you would need to maintain the rest of the buffer after
the terminator, and use that when trying to find the *next* terminator.
(Indeed, you may have read several responses in one go.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Hi Mike,

I think that first of all you have to read the NNTP RFC
I did a quick google search I got this http://www.faqs.org/rfcs/rfc977.html

There you will find explained how to "talk" with an NNTP server.

I think that it will be easier for you if you just use a component for this, try : http://groups.google.com/groups?q=NN...ie=UTF-8&hl=en

you will get a lot of post regarding C# and NNTP

btw, you should not create the buffer inside the while, in this way on each cycle you are creating a new array and wasting the previous one. Kinda of similar of what happened with the String in your previous code.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Mike" <no****@nospam. com> wrote in message news:Ol******** ******@TK2MSFTN GP11.phx.gbl...

Hi Ignacio,

Thanks for your answer. I am actually coding against a NNTP server, but the current code I have is slow retrieving messages, so I am trying to find another solution. I have tried this other code:

1: while (true)
2: {
3: byte[] received = new byte[base.ReceiveBuf ferSize];
4: stream.Read(rec eived, 0, received.Length );
5: b.Append(Encodi ng.ASCII.GetStr ing(received));
6: }

but when the iteration goes through line 4 the second time and the server only sent a single line, the application hangs. I could not find a way to stop the iteration when there is no data or all data has been read.

What am I doing wrong here?

Thanks
Mike


"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote in message news:Ot******** ******@TK2MSFTN GP12.phx.gbl...
Hi Mike,
The thing is is that you are reading raw data, there is no concept of "message" as a logic data organization. either you improve your protocol to include a way to indicate the end of message ; for example SMTP use a line with a single dot "." to indicate the end of the email being sent; or if you don;t have control over the protocol you can read the raw data and then later do the processing of it.
Also as Jon suggested you should not use String but StringBuilder.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Mike" <no****@nospam. com> wrote in message news:Ou******** ******@tk2msftn gp13.phx.gbl...

Hi,

I have an iteration to retrieve a number of messages from a server. Within this iteration, I am using the following code:

do
{
readBytes = base.GetStream( ).Read(received , 0, received.Length );
string textToAdd = Encoding.ASCII. GetString(recei ved, 0, readBytes);
myCompleteMessa ge = String.Concat(m yCompleteMessag e, textToAdd);
}
while(base.GetS tream().DataAva ilable == true);

The problem that I have seen is that not the entire content of the message is retrieved. For example, I first read the header and then the body, and when I read the second message, the header still contains some information that belonged to the previous message's body.

Any idea?

Thanks
Mike
Nov 16 '05 #6

Thanks for your suggestions. I think I finally made it work :-)

Mike
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message news:MP******** *************** *@msnews.micros oft.com...
Mike <no****@nospam. com> wrote:
Thanks for your answer. I am actually coding against a NNTP server,
but the current code I have is slow retrieving messages, so I am
trying to find another solution. I have tried this other code:

1: while (true)
2: {
3: byte[] received = new byte[base.ReceiveBuf ferSize];
4: stream.Read(rec eived, 0, received.Length );
5: b.Append(Encodi ng.ASCII.GetStr ing(received));
6: }

but when the iteration goes through line 4 the second time and the
server only sent a single line, the application hangs. I could not
find a way to stop the iteration when there is no data or all data
has been read.

What am I doing wrong here?


The client can never know when the server's going to send some more
data unless the connection has been closed. The above will actually
continue for ever, and normally you'd check whether Read returned 0
(for "connection closed"). That leads me to another problem you've got
in the code above - you're always *assuming* that Read will fill the
whole of your buffer. Your code *ought* to be more like:

byte[] buffer = new byte[base.ReceiveBuf ferSize];
while (true)
{
int read = stream.Read(buf fer, 0, buffer.Length);
if (read==0)
{
break;
}
b.Append (Encoding.ASCII .GetString(rece ived, 0, read));
}

That still won't solve your problem though, because it will hang while
the NNTP server waits for some more input from you (at least, it'll
hang until the server decides to time your connection out).

Depending on what command you're using, the response will be terminated
either at the end of a line or (IIRC) with a "." on a line on its own -
consult the relevant RFC for details. When you've found that
terminator, *that's* the point at which to take whatever your next
action is. Note that (possibly not for NNTP, but for other protocols)
you may end up reading into the next message or bit of response data,
in which case you would need to maintain the rest of the buffer after
the terminator, and use that when trying to find the *next* terminator.
(Indeed, you may have read several responses in one go.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

Hi Ignacio,

Thanks for your suggestions. It now works okay :-)

Mike
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote in message news:ei******** ******@TK2MSFTN GP12.phx.gbl...
Hi Mike,

I think that first of all you have to read the NNTP RFC
I did a quick google search I got this http://www.faqs.org/rfcs/rfc977.html

There you will find explained how to "talk" with an NNTP server.

I think that it will be easier for you if you just use a component for this, try : http://groups.google.com/groups?q=NN...ie=UTF-8&hl=en

you will get a lot of post regarding C# and NNTP

btw, you should not create the buffer inside the while, in this way on each cycle you are creating a new array and wasting the previous one. Kinda of similar of what happened with the String in your previous code.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Mike" <no****@nospam. com> wrote in message news:Ol******** ******@TK2MSFTN GP11.phx.gbl...

Hi Ignacio,

Thanks for your answer. I am actually coding against a NNTP server, but the current code I have is slow retrieving messages, so I am trying to find another solution. I have tried this other code:

1: while (true)
2: {
3: byte[] received = new byte[base.ReceiveBuf ferSize];
4: stream.Read(rec eived, 0, received.Length );
5: b.Append(Encodi ng.ASCII.GetStr ing(received));
6: }

but when the iteration goes through line 4 the second time and the server only sent a single line, the application hangs. I could not find a way to stop the iteration when there is no data or all data has been read.

What am I doing wrong here?

Thanks
Mike


"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote in message news:Ot******** ******@TK2MSFTN GP12.phx.gbl...
Hi Mike,
The thing is is that you are reading raw data, there is no concept of "message" as a logic data organization. either you improve your protocol to include a way to indicate the end of message ; for example SMTP use a line with a single dot "." to indicate the end of the email being sent; or if you don;t have control over the protocol you can read the raw data and then later do the processing of it.
Also as Jon suggested you should not use String but StringBuilder.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Mike" <no****@nospam. com> wrote in message news:Ou******** ******@tk2msftn gp13.phx.gbl...

Hi,

I have an iteration to retrieve a number of messages from a server. Within this iteration, I am using the following code:

do
{
readBytes = base.GetStream( ).Read(received , 0, received.Length );
string textToAdd = Encoding.ASCII. GetString(recei ved, 0, readBytes);
myCompleteMessa ge = String.Concat(m yCompleteMessag e, textToAdd);
}
while(base.GetS tream().DataAva ilable == true);

The problem that I have seen is that not the entire content of the message is retrieved. For example, I first read the header and then the body, and when I read the second message, the header still contains some information that belonged to the previous message's body.

Any idea?

Thanks
Mike
Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
13022
by: It's me | last post by:
Why do I get an "AttributeError: read" message when I do: import sys r=sys.stdin.read() ?? I've tried: r=sys.stdin.read(80)
4
2433
by: Bill Cohagan | last post by:
I'm writing a console app in c# and am encountering a strange problem. I'm trying to use redirection of the standard input stream to read input from a (xml) file. The following code snippet is from this app: =============================== static void Main(string args) { if (args.Length > 0) Console.SetIn(new StreamReader(args)); //executes if I don't use the "<", ">" redirection syntax when invoking XmlTextReader xmlin = new...
3
4375
by: Sandi | last post by:
I have a simple problem: I have an Access database (images.mdb) that has 2 columns: one is the id if the picture (an integer) and one (column named picture) is a field of type OLE Object which contains an image (it can store jpg, bmp, gif, but I don't know what image is stored inside). I want to retrieve the picture stored in the database and identified by a given id and display it in a web page (.aspx). I write in Visual C#, but it does...
1
4166
by: mike w. | last post by:
how can I handle when data is received from TcpClient.GetStream. i'd like an event raised when data arrives thanks, mike w.
5
5120
by: Sumana | last post by:
Hi All, We developed our project on VC++.Net console application to create image of disk and to write the image We are having problem with reading and writing the sector beyond 6GB Disk or Partition we are using ReadFile , WriteFile and setFilePointerEx to read and write the sectors and we are reading/writing 102400 sectors together, even we have reduced the sectors still it is not able to read or write, our program is working fine...
2
4064
by: Tim Lambert | last post by:
Has anyone seen this happen? I have seen it on a system that is making lots and lots of short lived connections. At this point I'm not sure of the high water mark for the number of connections. Maybe I'm just running out of some kind of resource and the .NET framework is not doing a good job at reporting it. Any other ideas on what might cause it? Thanks. timXXXlambertXXX@mindspringXXX.com
7
11716
by: ad | last post by:
I have an Excel file save in the MyResource.resx. I have to load the Excel file into strem. I use the code: ------------------------------------------------------------------------------------------ ResourceManager a = new ResourceManager("WillNs.MyResource", Assembly.GetExecutingAssembly()); Stream myStream = a.GetStream("Empty.xls"); ------------------------------------------------------------------------------------------ But mySteam...
1
3116
by: Ryan Liu | last post by:
Hi, Why TcpClient has a method TcpClient.GetStream(), not just a read only property? By implementing it as a method, does that mean, each time GetStream() could return a different stream? In other words, is that safe I only call GetStream() once and set it to a class' variable and reuse it later? And sometime I see TcpClient.GetStream() throws
0
810
by: abdulbasith | last post by:
how to read xml file sent from server to client machine through Beginread method in C# , can anyone help in storing that xml file in dataset.
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10163
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
10000
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...
0
9037
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
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...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
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.