Solved

Sockets/TcpClient: Reading with NetworkStream

Posted on 2006-11-18
6
575 Views
Last Modified: 2012-05-05
Hi.

I'm using NetworkStream to read data from the TcpClient, but it returns data in a weird way as aposed to StreamReader. It returns the whole bunch of data, where each line is supposed to be terminated.. StreamReader reads Line1, NetworkStream reads Line1,2,3,4,5,6 and returns those. Do I set up a TextReader to read from the NetworkStream or does that not make any sense to do that.

The problem is my function to handle the parsing of the data can only handle single, and hence only the first line of the whole buffer is being parsed.. data is formatted like:

C,something,something,something...

Output from NetWorkStream looks similar to (_In one read_):

C,something,something,something...
C,something,something,something...
C,something,something,something...
C,something,something,something...
C,something,something,something...

Output from StreamReader (not using async setup):

C,something,something,something...

In one read, then reads again and returns another line.

I'm puzzled.
0
Comment
Question by:valvet
6 Comments
 
LVL 8

Expert Comment

by:Razzie_
ID: 17970952
If I understand correctly, your problem is that the NetworkStream reads all the 6 lines at once, but you only want a single line of C,something,something,something...

To my knowledge, the NetworkStream cannot write one line at a time (it has no ReadLine() method) so if you use Read() it will always return everything.

So if you want a single line to be read, to my best knowledge you will have to use something other than NetworkStream.

HTH,
Razzie


0
 

Author Comment

by:valvet
ID: 17972105
I kinda of figured, and yes you did understand correctly.

Well, I do like the way it's being read with NetworkStream, as it seems to be a bit faster. I'm gonna have to change my parsing function then I suppose.
0
 
LVL 11

Accepted Solution

by:
Expert1701 earned 500 total points
ID: 17973013
> Do I set up a TextReader to read from the NetworkStream or does that not make any sense to do that.

  System.IO.StreamReader reader = new System.IO.StreamReader(networkStream);
  string line = reader.ReadLine();
  while (line != null)
  {
    //Process Line
    //...
    line = reader.ReadLine();
  }
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question