/// <summary> /// 处理接收到的数据 /// </summary> private void ReceiveHandler() { int len = _receiveByte64K.arrByte64K[0] * 256 + _receiveByte64K.arrByte64K[1]; if (len > 0 && _receiveByte64K.len >= len) { ArrByte64K bytes64k = ArrByte64K.Create(); bytes64k.AppendBytes(_receiveByte64K.arrByte64K, len); _receiveByte64K.DelBytes((ushort)len); ServerPacket serPkt = ServerPacket.Create(bytes64k); App_Event.HandleEvent(serPkt); } }
private void ReceiveCallback(IAsyncResult ar) { if (!IsconnectSuccess) { return; } try { //Debug.Log("ReceiveCallback..."); StateObject state = (StateObject)ar.AsyncState; Socket client = state.workSocket; if (client == null || !client.Connected) { Log("ReceiveCallback======!!!!!!"); return; } // Read data from the remote device. int bytesRead = client.EndReceive(ar); if (bytesRead > 0) { // There might be more data, so store the data received so far. _receiveByte64K.AppendBytes(state.buffer, bytesRead); //Debug.Log("ReceiveCallback... bytesRead > 0"); client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); } else { // Signal that all bytes have been received. // Receive the response from the remote device. Receive(clientSocket); } receiveDone.Set(); } catch (Exception e) { IsconnectSuccess = false; SockErrorStr = e.ToString(); } }