- Home /
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!!
Odd. Can you write byte values for both "GetBytes(dataToSend)" and "buffer"? $$anonymous$$ight worth reading the difference.
Answer by Fantilyre · Aug 02, 2017 at 09:49 AM
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 !
Answer by adrienPlayerium · Aug 02, 2017 at 09:02 AM
@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~
Thanks for the answer :) .
I tried String.Equals(), returns FALSE just like the == operator. Although with further testing I narrowed the issue to the buffer.Length . It returns a length of 255 while it should return a length of 3... I am not sure how to control the byte length is the same when it arrives as when it was sent though :/ . I could send a first byte that specifies the expected length ? Not sure.
Your answer
Follow this Question
Related Questions
Hosting external application on UI element 0 Answers
IAP Windows Universal 0 Answers
Windows Universal IAP 0 Answers
Coroutine works in editor, but not in build Windows application 0 Answers