private void ReceiveCallback(IAsyncResult ar) { try { // Retrieve the state object and the client socket // from the asynchronous state object. TCPState state = (TCPState)ar.AsyncState; Socket client = state.Socket; if (!IsConnected()) { Debug.Log("<color=red>不想接收 了</color>"); return; } // Read data from the remote device. int bytesRead = client.EndReceive(ar); //Debug.Log("get from server, length = " + bytesRead); if (bytesRead > 0) { byte[] realData = new byte[bytesRead]; Array.Copy(state.Buffer, realData, bytesRead); //Globals.SceneSingleton<SocketNetworkMng>().Handler(realData); // 拼接数据包 if (m_buffer.TargetLength < 0) { m_buffer.Init(realData); if (m_buffer.CheckComplete()) { CmdRequest.Handle(this, m_buffer.Buffer.ToArray()); m_buffer.Clear(); } } else { if (m_buffer.CheckComplete()) { CmdRequest.Handle(this, m_buffer.Buffer.ToArray()); m_buffer.Clear(); } else { m_buffer.Buffer.AddRange(realData); } } Receive(); } } catch (SocketException e) { Debug.Log(e.ToString()); } catch (Exception e) { Debug.LogError(e.ToString()); } }