locked
NetworkStream Peek vs DataAvailable RRS feed

  • Question

  • Why would peek() return a value > 0 but DataAvailable returns false?

    Subsequent ReadLine of the stream from a StreamReader times out.

    Monday, April 27, 2015 3:48 PM

Answers

  • I think this is a pretty good answer along with maybe the one above on peek vs dataAvailable.

    http://vadmyst.blogspot.com/2008/04/what-is-peeking-in-tcp-and-why-should.html

    • Marked as answer by chuck02323 Tuesday, May 5, 2015 9:28 PM
    Tuesday, May 5, 2015 9:28 PM

All replies

  • Hi chuck02323,

    I will move this thread to the C# forum, in there you will get good replies from the C# experts.

    Thanks for your understanding.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, April 28, 2015 7:11 AM
  • Hi chunk,

    >>Subsequent ReadLine of the stream from a StreamReader times out.

    I am not completely sure what you are trying to achieve. I would suggest you provide a simplified sample about this issue,  and do you have some error information?

    I suspect you are do a while loop with Peek() method, am i right? If not, please feel free to let me know.

    I would suggest that you can use the following code to have a try.

    NetworkStream ns = new NetworkStream(m_sck); 
     StreamReader  sr = new StreamReader(ns,); 
     StreamWriter  sw = new StreamWriter(ns); 
    
    sw.AutoFlush = true; 
    
    while( m_sck.Connected ) 
     { 
             Console.WriteLine("Starting Peek()..."); 
             Console.WriteLine("Peek(): " + sr.Peek().ToString()); 
             Console.WriteLine("Starting ReadLine()..."); 
             string command = sr.ReadLine(); 
             Console.WriteLine("ReadLine(): " + command); 
             sw.WriteLine(command); 
    
    } 

    Hope it is helpful for you.

    Have a nice day!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    • Edited by Kristin Xie Tuesday, April 28, 2015 8:37 AM
    Tuesday, April 28, 2015 8:36 AM
  • I did a network trace.

    I found that in the c# code I do a send.

    15 seconds later it shows up in the NetMonitor trace as sent.

    I disable Symantec Endpoint Protection, no change.

    Thursday, April 30, 2015 8:39 PM
  • I did a network trace.

    I found that in the c# code I do a send.

    15 seconds later it shows up in the NetMonitor trace as sent.

    I disable Symantec Endpoint Protection, no change.

    Could you post your code here? I cannot reproduce your issue. Also see my last reply, Do you do a while loop? Or putting peek() method in a loop?

    Please take a look this sample from MSDN, We often connect to use NetworkStream.Read Method with NetworkStream.DataAvailable Property.

    // Examples for CanRead, Read, and DataAvailable. 
    
                // Check to see if this NetworkStream is readable. 
                if(myNetworkStream.CanRead){
                    byte[] myReadBuffer = new byte[1024];
                    StringBuilder myCompleteMessage = new StringBuilder();
                    int numberOfBytesRead = 0;
    
                    // Incoming message may be larger than the buffer size. 
                    do{
                         numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);
    
    					 myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
    				 					
                    }
                    while(myNetworkStream.DataAvailable);
    
                    // Print out the received message to the console.
                    Console.WriteLine("You received the following message : " +
                                                 myCompleteMessage);
                }
                else{
                     Console.WriteLine("Sorry.  You cannot read from this NetworkStream.");
                }

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, May 4, 2015 9:24 AM
  • Peek is one thing, ReadLine is another thing and DataAvailable is yet another thing.

    Peek returns data from StreamReader's own buffer. Filling the reader buffer can consume all the data available on the socket and if that happens DataAvailable naturally returns false. As for ReadLine, that one has to wait until a newline character is read. Maybe there's data in the reader's buffer, maybe there's data to read from the socket but if that data doesn't contain a newline character then ReadLine will keep waiting.

    • Marked as answer by chuck02323 Tuesday, May 5, 2015 9:25 PM
    • Unmarked as answer by chuck02323 Tuesday, May 5, 2015 9:26 PM
    Monday, May 4, 2015 9:44 AM
  • I think this is a pretty good answer along with maybe the one above on peek vs dataAvailable.

    http://vadmyst.blogspot.com/2008/04/what-is-peeking-in-tcp-and-why-should.html

    • Marked as answer by chuck02323 Tuesday, May 5, 2015 9:28 PM
    Tuesday, May 5, 2015 9:28 PM