Solved

Sockets/TcpClient: Reading with NetworkStream

Posted on 2006-11-18
6
571 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_
Comment Utility
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
Comment Utility
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
Comment Utility
> 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

19 Experts available now in Live!

Get 1:1 Help Now