【问题标题】:if statement occuring while condition is false (System.Net)如果条件为假时发生的语句(System.Net)
【发布时间】:2019-11-10 19:43:34
【问题描述】:

我是新来的,所以如果这篇文章的格式很糟糕,我很抱歉。

所以我遇到的问题...

我正在制作一个基于 Tcp 的消息传递应用程序,类似于 Discord、Teamspeak 等。如下所示,我有一个函数返回一个从网络流中提取的 byte[]。我还有一个 if/else 语句,以确保该函数不会尝试从未连接的流中提取数据,因此我有一个 bool(已连接)来确定连接的状态。此布尔值已正确更新以匹配连接状态。起初我以为这可能是问题,但我通过调试发现它不是。


private byte[] RecieveData(TcpClient server)
        {
            byte[] data = new byte[1024];
            if (connected)
            {
6th line ->     server.GetStream().Read(data, 0, data.Length);
                return data;
            }
            else
            {
                return null;
            }
        }

Picture of debugging (cant add images for some reason)

我的问题是,为什么第 6 行代码 (server.GetStream().Read(data, 0, data.Length);) 在 if 条件为假时运行。 如果您需要我的任何东西(图片、代码等),请询问! 任何帮助将不胜感激。谢谢!

最小可重现示例

客户: 按执行顺序

private void ServerDisconnect(TcpClient server, byte[] data) //Called from a button
    {
        connected = false;
        Server = null;
        Disconnect(server);
    }

public void Disconnect(TcpClient server)// Checks for a connection, if there is, send DISCON request to server, if not dont
    {
        if (server.Connected) SendMessage(server, DISCON, currentUser);
        connected = false;
        server.Close();
    }

private void SendMessage(TcpClient server, byte code, User user)// Uses AddMessageCode method to specify what type of request the message is.
    {
        NetworkStream stream = server.GetStream();
        byte[] data = AddMessageCode(code, ObjectToByteArray(user));//Uses a simple Binary converter to serialize a class.
        stream.Write(data, 0, data.Length);//Sends request to server
    }

private byte[] AddMessageCode(byte code, byte[] data)// Adds the byte code to the start of the data array.
    {
        byte[] newData = new byte[data.Length + 1];
        newData[0] = code;
        Array.Copy(data, 0, newData, 1, data.Length);
        return newData;
    }

理论上,下面的方法应该不会导致错误。但确实如此。

private byte[] RecieveData(TcpClient server)
    {
        byte[] data = new byte[1024];
        if (server.Connected)
        {
            server.GetStream().Read(data, 0, data.Length);
            return data;
        }
        else
        {
            return null;
        }
    }

如果这还不够清楚。我道歉。

Link to source code

【问题讨论】:

  • 大概connected 是从另一个线程设置的,在这个线程上读取它之后但在调试器中断之前。
  • 如果你能分享一个minimal reproducible example,那就太棒了。
  • 我曾尝试使用 TcpClient 变量的 Connected 属性,但它会引发 dispose 错误。
  • 我希望您创建一个minimal reproducible example 并将其放入您的问题中,正如@mjwills 要求您做的那样
  • 如果我让你进屋,那之后我再关门也没关系。当你进来到我家(if)时,门是开着的。在该点之后查看门的状态(如调试器所示)告诉您没有任何用处。 最简单的解决方案可能是try catch(哦,哇,他们让我进去了,但现在房子爆炸了-最好返回null)。

标签: c# networking tcpclient


【解决方案1】:

在这个特定的实例中,if 语句已经在条件为真的情况下执行。我不明白的部分是stream.Read() 方法等到收到数据才能继续。

如果这没有意义,这里是@mjwills 的一个类比,

if 语句是房子,门是条件。如果您在门打开时进入房屋(条件为真),无论门是否打开(条件为真或假),您都在house(if 语句中的代码正在执行)。而在这种情况下,里面的代码不会很快完成,它会等待来自流的数据。

感谢 stackoverflow 社区在问题发布后的 10 分钟内帮助我理解这一点!

【讨论】:

    猜你喜欢
    • 2016-10-29
    • 1970-01-01
    • 2015-08-20
    • 2023-04-04
    • 2015-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    相关资源
    最近更新 更多