private static voidReceiveMessage()
{while (!cancelTokenSource.IsCancellationRequested)
{try{byte[] buffer = new byte[1024];int length =client.Receive(buffer);if (length > 0)
{
StringBuilder sOutput= newStringBuilder(buffer.Length);for (int i = 0; i< buffer.Length; i++)
{
sOutput.Append(buffer[i].ToString("X2"));
}var result =ByteArrayToHexString(buffer);if (result.Contains("00 00 00 13 01 00 12"))
{
Console.WriteLine("登录成功");
}else if (result.Contains("00 00 00 13 01 00 03"))
{
Console.WriteLine("接收心跳");
}else if (result.Contains("00 00 00 18 00 02 00"))
{
Console.WriteLine("门状态变化");
}else if (result.Contains("00 00 00 18 00 02 01"))
{
Console.WriteLine("控制器状态");
}else if (result.Contains("00 00 00 18 00 02 02"))
{
Console.WriteLine("刷卡记录");
}else if (result.Contains("00 00 00 18 00 03 00"))
{
Console.WriteLine("访客刷卡");
}else{string datapacket = "00000013010200000000000000000101C46AF0";byte[] array =HexStringToByteArray(datapacket);
client.Send(array);byte[] newByte =LongToShort(buffer);string res =Encoding.UTF8.GetString(newByte);
LiFangMsg lf= JsonConvert.DeserializeObject(res);
Console.WriteLine(res);
}
}
}catch(Exception e)
{
Console.WriteLine("发生错误:" +e.Message);
}
}
}