Why is the stream connector connected to a network stream returning null in ReadLine ()?

The code below has a StreamReader read from a network stream. This code will work fine for several days. I ran into a problem when suddenly StreamReader.ReadLine () started returning null.

According to the documentation of Microsoft StreamReader.ReadLine () will return null when it reaches the end of the input stream. This does not make sense to me when the underlying stream is NetworkStream. Should ReadLine () not be blocked until the network stream receives data?

This is the first time I have encountered this problem and I have not been able to duplicate it. What could be the reason for this?

Context: An application receives CDRs from a telephone switch. The phone switch connects to the application and sends plain old text entries. After the switch is connected, it will remain connected and will continue to send records for eternity if something does not break.

    private void ProcessClient(TcpClient client)
    {
        try
        {
            using (NetworkStream stream = client.GetStream())
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    //continue processing while service is on
                    while (m_RunService & client.Connected)
                    {

                        string curLine = reader.ReadLine();

                        //code here does stuff to string
                        //will catch any exceptions that have to do with 
                        //processing the string
                    }
                }
            }
        }
        catch (Exception ex)
        {
            //write to log
        }
    }

Here is the code that starts the listener:

    private void Listen()
    {
        try
        {
            while (m_RunService)
            {
                try
                {
                    m_TcpClient = m_TcpListener.AcceptTcpClient();

                    //run on same thread, should only ever be 1 cnx at a time
                    ProcessClient(m_TcpClient);
                }
                catch (Exception ex)
                {
                    //write to log
                }
                finally
                {
                    m_TcpClient.Close();
                }
            }
        }
        finally
        {
            m_TcpListener.Stop();
        }        
    } 
+3
source share
3 answers

StreamReaderwill be blocked until it receives data or the connection is closed. It seems that the exception occurred on the server side, it closed the connection, and the client side did not receive data.

+4
source

It looks like the stream is closed. Otherwise, yes: it is blocked. My guess is: network #fail

0

NetworkStream , ReadLine() null, , .

NetworkStream.CanRead NetworkStream.DataAvailable ReadLine. , - , .

, , Send Socket:

Connected -. false, Socket .

The value of the Connected property reflects the state of the connection since the last operation. If you need to determine the current connection status, a non-blocking, zero byte call. If the call returns successfully or issues an WAEWOULDBLOCK (10035) error code, then the socket is still connected; otherwise the socket will not be longer.

0
source

Source: https://habr.com/ru/post/1771761/


All Articles