Weird string compare behaviour with socket
I'm working on a socket connection between Python (which acts as the server) and Unity which acts as a client. I've been setting up commands like 'p', 'rotate', 'set amount {int}' and a few others. When I get the server command in Unity I split this on spaces so I can figure out which function to call on the Unity side.
Python sends the command with c.send(bytes(message, "utf-8"))
and Unity catches it with client.BeginReceive(buffer, 0, buffer.Length, 0, new AsyncCallback(callback), client);
, after that converting it to string with response = Encoding.UTF8.GetString(buffer);
.
However there's some strange behaviour which I don't understand why it occurs and how to fix it: I can't directly compare the server input (or one of it's splitted parts) to a string, like command[0] == "rotate"
, it only works with command[0].Contains("rotate")
. I've tried trimming the command (and it's splitted parts) but that don't work. Comparing server command vs. trimmed server command returns true. Logging the command (part) shows the correct text, so I really don't know why this wouldn't work.
(There is some weird behaviour with the buffer which I need to fix too but I don't think it's related to the above problem, basically if I send two commands, 'set amount 20' and 'set amount 1', the second response in Unity is received as 'set amount 10')