Strange behaviour of StreamReader.Peek()
-
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?
-
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?
"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.
-
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?
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!
-
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?
NetworkStream.DataAvailable
top secret
Download xacc-ide 0.0.3 now!
See some screenshots -
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?
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!!
-
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!!
Search for Sockets here on CodeProject and/or read the example given by the MSDN topics for the Socket class.