473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

GetStream().Read 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(received, 0, readBytes);
myCompleteMessage = String.Concat(myCompleteMessage, textToAdd);
}
while(base.GetStream().DataAvailable == 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 6415
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(received, 0, readBytes);
myCompleteMessage = String.Concat(myCompleteMessage, textToAdd);
}
while(base.GetStream().DataAvailable == 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.com>
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**************@tk2msftngp13.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(received, 0, readBytes);
myCompleteMessage = String.Concat(myCompleteMessage, textToAdd);
}
while(base.GetStream().DataAvailable == 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.ReceiveBufferSize];
4: stream.Read(received, 0, received.Length);
5: b.Append(Encoding.ASCII.GetString(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.machin AT dot.state.fl.us> wrote in message news:Ot**************@TK2MSFTNGP12.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**************@tk2msftngp13.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(received, 0, readBytes);
myCompleteMessage = String.Concat(myCompleteMessage, textToAdd);
}
while(base.GetStream().DataAvailable == 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.ReceiveBufferSize];
4: stream.Read(received, 0, received.Length);
5: b.Append(Encoding.ASCII.GetString(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.ReceiveBufferSize];
while (true)
{
int read = stream.Read(buffer, 0, buffer.Length);
if (read==0)
{
break;
}
b.Append (Encoding.ASCII.GetString(received, 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.com>
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**************@TK2MSFTNGP11.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.ReceiveBufferSize];
4: stream.Read(received, 0, received.Length);
5: b.Append(Encoding.ASCII.GetString(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.machin AT dot.state.fl.us> wrote in message news:Ot**************@TK2MSFTNGP12.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**************@tk2msftngp13.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(received, 0, readBytes);
myCompleteMessage = String.Concat(myCompleteMessage, textToAdd);
}
while(base.GetStream().DataAvailable == 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.com> wrote in message news:MP************************@msnews.microsoft.c om...
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.ReceiveBufferSize];
4: stream.Read(received, 0, received.Length);
5: b.Append(Encoding.ASCII.GetString(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.ReceiveBufferSize];
while (true)
{
int read = stream.Read(buffer, 0, buffer.Length);
if (read==0)
{
break;
}
b.Append (Encoding.ASCII.GetString(received, 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.com>
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.machin AT dot.state.fl.us> wrote in message news:ei**************@TK2MSFTNGP12.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**************@TK2MSFTNGP11.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.ReceiveBufferSize];
4: stream.Read(received, 0, received.Length);
5: b.Append(Encoding.ASCII.GetString(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.machin AT dot.state.fl.us> wrote in message news:Ot**************@TK2MSFTNGP12.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**************@tk2msftngp13.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(received, 0, readBytes);
myCompleteMessage = String.Concat(myCompleteMessage, textToAdd);
}
while(base.GetStream().DataAvailable == 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
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
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...
3
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...
1
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
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...
2
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. ...
7
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:...
1
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...
0
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...

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.