public static void getResponseData(IAsyncResult ar) { try { //Socket clientSocket = (Socket)ar.AsyncState; object[] array = (object[])ar.AsyncState; Socket clientSocket = (Socket)array[0]; clientSocket.EndSend(ar); byte[] byteData = new byte[1024]; clientSocket.Receive(byteData); string strReceived = Encoding.UTF8.GetString(byteData); Array.Clear(byteData, 0, byteData.Length); int i = strReceived.IndexOf("\0"); if (i > 0) { string data = strReceived.Substring(0, i); Debug.WriteLine(" Data => SP: " + data); //todo here should deal with the received string IDeviceCommand cmd = (IDeviceCommand)array[1]; cmd.callBack(data); } } catch (Exception e) { Debug.WriteLine("getResponseData => " + e.Message); } }
void parseCommand(string strReceived) { if (strReceived.Length <= 0 || (strReceived.Length % 2) != 0) { return; } for (int i = 0; i < strReceived.Length; i += 2) { string temp = strReceived.Substring(0, i + 2); IDeviceCommand cmd = DeviceCommandManager.getDeivceCommandWithResponseOf(temp); if (cmd != null) { Debug.WriteLine("parseCommand ok => " + temp); cmd.callBack(temp); builder.Remove(0, i + 2); parseCommand(strReceived.Substring(i + 1)); } else { Debug.WriteLine("parseCommand null => " + temp); } } }