Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Strange behaviour of StreamReader.Peek()

Strange behaviour of StreamReader.Peek()

Scheduled Pinned Locked Moved C#
sysadminhelpquestiondiscussion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SebbaP
    wrote on last edited by
    #1

    Hi! I am writing a server/client application in which I need to transfer short text strings across a network from the server to the client. The message receiver in the client is running in its own thread, and it uses a stream reader to get the message strings from the TcpClient. It appears that both the 'Read' and 'ReadLine' methods of the StreamReader class blocks until there is something on the stream (if it is empty when they are invoked). The problem is that not even a call to .Abort() will interrupt the Read(Line) metod (which is exactly what I want to do). Is there any way to abort a call to Read(Line)? I have tried to use the Peek() method to determie whether or not the stream is empty. However, Peek() returns -1 even though ReadLine() returns with a string that is supposed to be there. Any thoughts on why this happens? Does anyone no of a construct that provides a non-blocking read from a stream?

    S D L S 4 Replies Last reply
    0
    • S SebbaP

      Hi! I am writing a server/client application in which I need to transfer short text strings across a network from the server to the client. The message receiver in the client is running in its own thread, and it uses a stream reader to get the message strings from the TcpClient. It appears that both the 'Read' and 'ReadLine' methods of the StreamReader class blocks until there is something on the stream (if it is empty when they are invoked). The problem is that not even a call to .Abort() will interrupt the Read(Line) metod (which is exactly what I want to do). Is there any way to abort a call to Read(Line)? I have tried to use the Peek() method to determie whether or not the stream is empty. However, Peek() returns -1 even though ReadLine() returns with a string that is supposed to be there. Any thoughts on why this happens? Does anyone no of a construct that provides a non-blocking read from a stream?

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      "Any thoughts on why this happens?": MSDN states for the return value of the Peek method: "The next character to be read, or -1 if no more characters are available or the stream does not support seeking." So maybe your stream doesn't support seeking.? "Does anyone no of a construct that provides a non-blocking read from a stream?": Try getting the underlying stream through BaseStream property and using its asynchronous BeginRead and EndRead methods.


      www.troschuetz.de

      1 Reply Last reply
      0
      • S SebbaP

        Hi! I am writing a server/client application in which I need to transfer short text strings across a network from the server to the client. The message receiver in the client is running in its own thread, and it uses a stream reader to get the message strings from the TcpClient. It appears that both the 'Read' and 'ReadLine' methods of the StreamReader class blocks until there is something on the stream (if it is empty when they are invoked). The problem is that not even a call to .Abort() will interrupt the Read(Line) metod (which is exactly what I want to do). Is there any way to abort a call to Read(Line)? I have tried to use the Peek() method to determie whether or not the stream is empty. However, Peek() returns -1 even though ReadLine() returns with a string that is supposed to be there. Any thoughts on why this happens? Does anyone no of a construct that provides a non-blocking read from a stream?

        D Offline
        D Offline
        Daniel Turini
        wrote on last edited by
        #3

        You'll need to use asynchronous interfaces. Use the BeginRead/EndRead, BeginWrite/EndWrite methods. Not THAT easy to do, but once you learn the asynchronous patterns in .NET, everything will be easier. My advice: don't use TcpClient/TcpServer for anything but the simplest things, as they're too simple, and have several drawbacks, as you're starting to notice. Use the Socket class, instead, that is a bit more complex to use, but will not give you so many headaches. Yes, even I am blogging now!

        1 Reply Last reply
        0
        • S SebbaP

          Hi! I am writing a server/client application in which I need to transfer short text strings across a network from the server to the client. The message receiver in the client is running in its own thread, and it uses a stream reader to get the message strings from the TcpClient. It appears that both the 'Read' and 'ReadLine' methods of the StreamReader class blocks until there is something on the stream (if it is empty when they are invoked). The problem is that not even a call to .Abort() will interrupt the Read(Line) metod (which is exactly what I want to do). Is there any way to abort a call to Read(Line)? I have tried to use the Peek() method to determie whether or not the stream is empty. However, Peek() returns -1 even though ReadLine() returns with a string that is supposed to be there. Any thoughts on why this happens? Does anyone no of a construct that provides a non-blocking read from a stream?

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          NetworkStream.DataAvailable top secret
          Download xacc-ide 0.0.3 now!
          See some screenshots

          1 Reply Last reply
          0
          • S SebbaP

            Hi! I am writing a server/client application in which I need to transfer short text strings across a network from the server to the client. The message receiver in the client is running in its own thread, and it uses a stream reader to get the message strings from the TcpClient. It appears that both the 'Read' and 'ReadLine' methods of the StreamReader class blocks until there is something on the stream (if it is empty when they are invoked). The problem is that not even a call to .Abort() will interrupt the Read(Line) metod (which is exactly what I want to do). Is there any way to abort a call to Read(Line)? I have tried to use the Peek() method to determie whether or not the stream is empty. However, Peek() returns -1 even though ReadLine() returns with a string that is supposed to be there. Any thoughts on why this happens? Does anyone no of a construct that provides a non-blocking read from a stream?

            S Offline
            S Offline
            SebbaP
            wrote on last edited by
            #5

            Thank you all for your advice! I will switch to using sockets instead. It will take some effort, but I'm sure it's worth it! Does anyone know of a good tutorial on working with sockets? However, I'm the sort of person who cannot let things go so easily. That Peek() does not work as I think it would :wtf: still borthers me somewhat, and I would like an explanation if there is one? I will let this go eventually, but until then I will remain confused. Anyone? Thanks!!

            S 1 Reply Last reply
            0
            • S SebbaP

              Thank you all for your advice! I will switch to using sockets instead. It will take some effort, but I'm sure it's worth it! Does anyone know of a good tutorial on working with sockets? However, I'm the sort of person who cannot let things go so easily. That Peek() does not work as I think it would :wtf: still borthers me somewhat, and I would like an explanation if there is one? I will let this go eventually, but until then I will remain confused. Anyone? Thanks!!

              S Offline
              S Offline
              Stefan Troschuetz
              wrote on last edited by
              #6

              Search for Sockets here on CodeProject and/or read the example given by the MSDN topics for the Socket class.


              www.troschuetz.de

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups