4

I need to check if my client has something in the buffer so I did not wait to write it.

private async void ThreadMethod_ListenClient()
    {
        while (true) {
            if (ClientQueue.Count == 0)
                continue;
            Client client = (Client)ClientQueue.Dequeue ();
            if (client.Reader != null) {
                string Say = await client.Reader.ReadLineAsync();
                if (Say != null) {
                    if (Say.Length != 0) {
                        Console.WriteLine (Say);
                    }
                }
            }
            ClientQueue.Enqueue (client);

        }
    }

client.Reader : StreamReader from TcpClient

client.Socket : TcpClient

How to check if 'client.Reader' empty?

obs. C# with Mono

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523

3 Answers3

4

Also StreamReader.Peek() which returns -1 when end of file is reached or when there's nothing to read. Can be checked as follows:

while(StreamReader.Peek() != -1){
    //Your code here
}
Ahmed Anwar
  • 688
  • 5
  • 25
2

How to check if I have something to read on a StreamReader?

I don't know for StreamReader, but for TcpClient you can use TcpClient.GetStream() method which returns NetworkStream which in turn has DataAvailable property. This is explained in the Remarks section of the TcpClient.GetStream documentation:

You can avoid blocking on a read operation by checking the DataAvailable property.

Edit: I've posted this because your code is running into a specific domain. While the general approach described in other answers/comments work for normal finite streams (like FileStream, MemoryStream etc.), it doesn't work for infinite streams like NetworkStream.

Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
  • StreamReader a = new StreamReader (client.GetStream ( )) made a StreamReader for this socket. I am only working on StreamReader :) – Marlon Henry Schweigert Dec 27 '15 at 13:59
  • But still underlying `Stream` is a `NetworkStream`. `StreamReader` is just an abstraction on top of the stream. – Ivan Stoev Dec 27 '15 at 14:00
  • Slow? `((NetworkStream)client.Reader.BaseStream).DataAvailable` – Ivan Stoev Dec 27 '15 at 14:17
  • Yes... A make a NetworkStream on Client class for dont get everytime... But this dont show all lines now :( – Marlon Henry Schweigert Dec 27 '15 at 14:24
  • Well, the above just answers your question **how** to check. It doesn't mean you have to use it - your code is perfect for a listener, and `async/await` do the necessary magic to make it efficient. It's similar to message loop - you can check if there is a message in the message queue, but if there isn't at this time, that doesn't mean there will not be in the future. – Ivan Stoev Dec 27 '15 at 14:40
  • Thanks for your help :) – Marlon Henry Schweigert Dec 27 '15 at 15:23
1

StreamReader.ReadLine() method returns null if the end of stream has reached or if there is nothing left to read. So, you can check for that to verify whether there is anything to read or not.

if (client.Reader != null && client.Reader.ReadLine() != null)
Rahul
  • 76,197
  • 13
  • 71
  • 125