/// <summary> /// 监听异步回传 /// </summary> /// <param name="ar"></param> private void ReadCallback(IAsyncResult ar) { String content = String.Empty; Socketoutput socketoutput = (Socketoutput)ar.AsyncState; StateObject state = (StateObject)socketoutput.stateobject; try { Socket handler = state.workSocket; int bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { //转换为16进制 for (int i = 0; i < bytesRead; i++) { content += string.Format("{0:x2} ", state.buffer[i]); //注意空格 } PrintRecvMssg(content.Trim()); } } catch (Exception ex) { if (this.OutPut != null) { OutPut(string.Format("{0}数据解析错误:{1}", this.FacilityNumber, ex.Message), eOutputType.Error); } } }
/// <summary> /// 监听异步回传 /// </summary> /// <param name="ar"></param> private void ReadCallback(IAsyncResult ar) { String content = String.Empty; Socketoutput socketoutput = (Socketoutput)ar.AsyncState; StateObject state = (StateObject)socketoutput.stateobject; try { Socket handler = state.workSocket; int bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { //转换为16进制 for (int i = 0; i < bytesRead; i++) { content += string.Format("{0:x2} ", state.buffer[i]); //注意空格 } PrintRecvMssg(content); } } catch (Exception ex) { SetListenerState(false); if (this.OutPut != null) { OutPut(string.Format("ReadCallback,原因:{0}", ex.ToString()), eOutputType.Error); } } }
public void StartListening(Socket listener, Action <string, eOutputType> output) { while (true) { allDone.Reset(); Socketoutput socketoutput = new Socketoutput(); socketoutput.socket = listener; socketoutput.Output = output; listener.BeginAccept(new AsyncCallback(AcceptCallback), socketoutput); allDone.WaitOne(); } }
/// <summary> /// 监听事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { try { if (!this.listener.Connected) { return; } timer1.Stop(); #region 异步监听 Socketoutput socketoutput = new Socketoutput(); socketoutput.socket = this.Listener; socketoutput.Output = this.OutPut; StateObject state = new StateObject(); state.workSocket = this.Listener; socketoutput.stateobject = state; this.Listener.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), socketoutput); #endregion #region 步监听 //byte[] buffer = new byte[] { }; //string content = string.Empty; //this.Listener.Receive(buffer); //if (buffer.Length > 0) //{ // //转换为16进制 // for (int i = 0; i < buffer.Length; i++) // { // content += string.Format("{0:x2} ", buffer[i]);//注意空格 // } // this.IsListener = true; // PrintRecvMssg(content.Trim()); //} #endregion } catch { } finally { timer1.Start(); } }
private void AcceptCallback(IAsyncResult ar) { allDone.Set(); Socketoutput socketoutput = (Socketoutput)ar.AsyncState; Socket listener = (Socket)socketoutput.socket; try { Socket handler = listener.EndAccept(ar); StateObject state = new StateObject(); state.workSocket = handler; socketoutput.stateobject = state; handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), socketoutput); } catch (Exception ex) { socketoutput.Output(string.Format("AcceptCallback,原因:{0}", ex.ToString()), eOutputType.Error); } }
/// <summary> /// 监听事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { try { timer1.Stop(); Socketoutput socketoutput = new Socketoutput(); socketoutput.socket = this.Listener; socketoutput.Output = this.OutPut; StateObject state = new StateObject(); state.workSocket = this.Listener; socketoutput.stateobject = state; this.Listener.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), socketoutput); } catch { SetListenerState(false); } //timer1.Start(); }
private void ReadCallback(IAsyncResult ar) { String content = String.Empty; Socketoutput socketoutput = (Socketoutput)ar.AsyncState; StateObject state = (StateObject)socketoutput.stateobject; try { Socket handler = state.workSocket; int bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead)); content = state.sb.ToString(); PrintRecvMssg(content, socketoutput.interfacetype_chsb, socketoutput.Output); } } catch (Exception ex) { socketoutput.Output(string.Format("ReadCallback,原因:{0}", ex.ToString()), eOutputType.Error); } }