Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sockets/TcpClient: Reading with NetworkStream

Posted on 2006-11-18
6
Medium Priority
?
593 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
3 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

782 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