UTF8 string acquired through NAMEDPIPES unrecognized by Unityengine

Hi,

the problem is quite simple yet I can’t find a solution. I have a string value “sam” that I send through named pipes to my unity application. I receive the string and can print it in the log without issues, but if I use that string to compare with another string of same value created in unity itself (using IF or SWITCH functions), unity finds they don’t match.

My code, server side (Winform app):

dataWriter("sam");

void dataWriter(string dataToSend)
        {
            byte[] _buffer = Encoding.UTF8.GetBytes(dataToSend);
            pipeServer.BeginWrite(_buffer, 0, _buffer.Length, new AsyncCallback(asyncWriter), pipeServer);
        }

        private void asyncWriter(IAsyncResult result)
        {
            pipeServer = (NamedPipeServerStream)result.AsyncState;
            pipeServer.EndWrite(result);
            pipeServer.WaitForPipeDrain();
            pipeServer.Flush();
        }

On the client side (unity app) :

if (pipeStream.IsConnected)
        {
            Debug.Log("Start Reading");
            pipeStream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(readCallBack), pipeStream);
        }

private void readCallBack(IAsyncResult result)
    {

        pipeStream.EndRead(result);
        string stringData = Encoding.UTF8.GetString(buffer, 0, buffer.Length);

        Debug.Log("Data received : " + stringData); /// Will successfully print "Data received : sam" in unity logs
        if (stringData == "sam")   //// Will be FALSE
        {
            Debug.Log("Data received = sam");
        }
        else
        {
            Debug.Log("Data received does not equal SAM "); // Will print this, because it finds stringData is not equal to "sam"
        }
    }

I tried alot of things: change the encoding format, tried to transform the IF variable from string to byte to string using UTF8 encoding in hopes of having the same string as the data received, tried to trim the BOM from the string if there was any but apparently there is none already, etc.

Please save me from hell on earth!!

@Fantilyre , you can check if String.Equals() give you false as well, look into == operator as well, in the debugger if you can step by step, to see which assertion fails maybe there is /0 character or something ~~ Good luck~

Ok, well, solved it I guess. Its always when you come to ask for help stuff starts sorting itself out.

Like said in my answer to AdrienPlayerium, the issue was with the byte length. For now I send one byte more specifying the expected length of my string and I only keep that part and it works. I will need to change my solution in the near future because right now it will only work for informations that are no longer than 9 bytes total.

Server :

void dataWriter(string dataToSend)
        {
            int dataLen = dataToSend.Length;
            byte[] buffer = new byte[dataLen+1];
            string dataL = dataLen.ToString()+dataToSend;
            buffer = Encoding.UTF8.GetBytes(dataL);
            pipeServer.BeginWrite(buffer, 0, buffer.Length, new AsyncCallback(AsyncSend), pipeServer);
        }

Client :

string stringData = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
int dataLen = Convert.ToInt32(Encoding.UTF8.GetString(buffer, 0, 1));
string goodData = Encoding.UTF8.GetString(buffer, 1, dataLen);

So basically later on I will do it another way, which is have a specific char to split my data (for example “-”), use the part before the char as my expected byte length and the part after the char being the actual data I want to send/receive.

Thanks anyway !