Solved

Sockets/TcpClient: Reading with NetworkStream

Posted on 2006-11-18
6
573 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now