jeudi 1 mars 2018

c#: if (String.IsNullOrEmpty(myString)) still working for empty strings

my goal is to get a message from an other client over a server and work on with that message. Server and other client are in visual basic and if i try to communicate between two vb clients everything is fine, but i need the client in c# for my Unityproject. My Problem is that there are still empty Messages at the Console, so i think the if() doesn't work correct. Here is the relevant part of the Code:

try
    {
        do
        {   
            serverMsg = "";             
            try
            {
                theStream = mySocket.GetStream();
                Byte[] inStream = new Byte[mySocket.SendBufferSize];
                theStream.Read(inStream, 0, inStream.Length);
                serverMsg += Encoding.ASCII.GetString(inStream);
                serverMsg.Trim();

                //if ((serverMsg == null || serverMsg == ""))
                if (String.IsNullOrEmpty(serverMsg) ||serverMsg.Trim().Length == 0)                    
                {
                    // do nothing
                }
                else
                {
                    Debug.Log("[SERVER] -> " + serverMsg);

                }
            }
            catch (Exception e)
            {
                Debug.Log("[Fehler]" + e);
            }
        } while (socketReady == true); // repeat as long as connection to the server is ready
    }
    catch (Exception e)
    {
        Debug.Log("[Fehler]" + e);
    }

My Question seems to be similar to this one C# After string is sent via Networkstream, it can be read but doesn't work with “if” but i can't figure out how i could solve my Problem with String.Compare. Do you may have any hints or advice for me?

Aucun commentaire:

Enregistrer un commentaire