我使用Unity工具,在使用Socket类的时候,发现一个问题
下面是发送消息的方法
void StartClientReceive(string str){
//存在Client
//向客户端发送命令
if(clientSocketList.Count != 0){
for(int i = 0;i<clientSocketList.Count;i++){
clientSocketList[i].Send(Encoding.UTF8.GetBytes(str));
Debug.Log("向客户端" + clientSocketList[i].RemoteEndPoint + "发送了" + str + "的命令");
}
}else{
Debug.Log("没有客户端连接");
}
}
下面是接收消息的方法
void ReceiveMsg()
{
while(true){
if (clientSocket.Connected == false)
{
Debug.Log("与服务器断开了连接");
}
int lenght = 0;
byte[] data = new byte[1024];
lenght = clientSocket.Receive(data);
Debug.LogWarning("data is " + data);
Debug.LogWarning(lenght);
string str = Encoding.UTF8.GetString(data);
if (str.Contains("play"))
{
VideoCommond.instance.PlayRunable();
}else if (str.Contains("pause"))
{
VideoCommond.instance.PauseRunable();
}else if (str.Contains("stop"))
{
VideoCommond.instance.StopRunable();
}else if(str == "change"){
//问题出在这个Log一直打印不出来
Debug.LogWarning("Change");
}
Debug.LogWarning("收到命令" + str + str.Length);
}
}
可以看到在接收的地方我用 str == string 做判断就是不会执行,但是最后的收到命令那里输出的String确实是和判断的时候是一样的,那为什么直接用等于做判断会失效呢?是编码问题?还是data数据问题?怎么产生的这种问题?希望有人给我解答一下!谢谢!很困挠我。