为什么流读取器连接到网络流,在 ReadLine() 上返回 null? [英] Why is streamreader hooked up to networkstream returning null on ReadLine()?

查看:23
本文介绍了为什么流读取器连接到网络流,在 ReadLine() 上返回 null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我有一个从网络流读取的 StreamReader.此代码通常可以正常运行数天.我遇到了一个问题,突然 StreamReader.ReadLine() 开始返回 null.

In the code below I have a StreamReader reading from a network stream. This code normally will run fine for days. I ran into a problem where all of a sudden StreamReader.ReadLine() started returning null.

根据 Microsoft 文档,StreamReader.ReadLine() 在到达输入流的末尾时将返回 null.当底层流是 NetworkStream 时,这对我来说没有意义.ReadLine() 不应该阻塞直到网络流接收到数据吗?

According to Microsoft documentation StreamReader.ReadLine() will return null when it has reached the end of the input stream. This doesn't make sense to me when the underlying stream is a NetworkStream. Shouldn't ReadLine() just block until the network stream receives data?

这是我第一次遇到这个问题,我无法复制它.什么可能导致这种情况?

This is the first time I ran into this problem and I have not been able to duplicate it. What could cause this?

上下文:应用程序从电话交换机接收 CDR 记录.电话交换机连接到应用程序并发送纯旧文本记录.交换机连接后,它将保持连接并永久发送记录,除非出现问题.

Context: the application receives CDR records from a phone switch. The phone switch connects to the application and sends plain old text records. After the switch connects it will remain connected and keep sending records for eternity unless something breaks.

    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();
        }        
    } 

推荐答案

StreamReader 将阻塞,直到它接收到数据连接关闭.听起来好像是服务器端发生了异常,关闭了连接,客户端没有收到数据.

The StreamReader will block until it receives data or the connection is closed. It sounds like an exception occurred at the server side, it closed the connection, and the client side received no data.

这篇关于为什么流读取器连接到网络流,在 ReadLine() 上返回 null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆