Search code examples
c#network-programmingtcpclient

if statement occuring while condition is false (System.Net)


I'm new here so if this post is formatted terribly, I apologise.

So the issue I'm having...

I am making a Tcp Based messaging app similar to Discord, Teamspeak etc. As you can see below, I have a function that returns a byte[] which is pulled from the network stream. I also have an if/else statement to ensure that the function does not try to pull data from a stream that is not connected so I have a bool (connected) that determines the state of the connection. This bool is properly updated to match the connection status. I thought this may have been the problem at first but I have discovered through debugging that it is not.


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)

My question is, why does 6th line of code (server.GetStream().Read(data, 0, data.Length);) run when the if-condition is false. If you need anything from me (pics, code etc.) just ask! Any help will be greatly appreciated. Thanks!

Minimal Reproducible Example

Client: In order of execution

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

In theory, the below method should not cause an error. But it does.

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

If this still wasnt clear enough. I apologise.

Link to source code


Solution

  • In this particular instance, the if statement had already executed with the condition being true. The part I did not understand was that the stream.Read() method waits until data has been received to continue.

    If that didnt make sense, here is an analogy by @mjwills,

    The if statement is a house and the door is the condition. If you came into the house while the door was open (condition was true) no matter if the door is open or not (condition is true or false) you are in the house (the code inside the if statement is executing). And in this instance, the code inside does not complete quickly, it waits for data from the stream.

    Thanks stackoverflow community for helping me understand this in within 10 mins of the question posting!