private void ReceiveCallback(IAsyncResult ar) { try { // Retrieve the state object and the client socket // from the asynchronous state object. StateObject state = (StateObject)ar.AsyncState; Socket client = state.workSocket; // 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. state.sb.Clear(); state.sb.Append(Encoding.Default.GetString(state.buffer, 0, bytesRead)); // All the data has arrived; put it in response. if (state.sb.Length > 1) { string content = state.sb.ToString(); SocketDataReceivedArgs e = new SocketDataReceivedArgs(content); if (content.IndexOf("<STOP>") > -1) { this.Close(); return; } else if (content.IndexOf("START_LOG") > -1) { if (this.StartLogging != null) { this.StartLogging(this, e); } } else if (content.IndexOf("STOP_LOG") > -1) { if (this.StopLogging != null) { this.StopLogging(this, e); } } else if (this.DataReceived != null) { this.DataReceived(this, e); } } // Get the rest of the data. client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
private void async_DataReceived(object sender, SocketDataReceivedArgs e) { byte[] data = Encoding.Default.GetBytes(e.Data); #if RADAR if (!_radar.IsConnected) { _async.Send(Encoding.Default.GetBytes("Command Failed: Radar Disconnected")); return; } else if ((data[0] == 0xA5) && (data[1] == 0xA5)) //Radar Command. Send the data directly to the radar { _radar.Write(data, 0, data.Length); } #endif if ((data[0] == 0xA5) && (data[1] == 0xA5)) { ProcCmd(data); } }
private void async_StopLogging(object sender, SocketDataReceivedArgs e) { StopLogging(); }