/// <summary> /// 客户端通讯回调函数 /// </summary> /// <param name="ar"></param> private void TCPCallBack(IAsyncResult ar) { LeafTCPClient client = (LeafTCPClient)ar.AsyncState; if (client.NetWork.Connected) { try { NetworkStream ns = client.NetWork.GetStream(); byte[] recdata = new byte[ns.EndRead(ar)]; if (recdata.Length > 0) { Array.Copy(client.buffer, recdata, recdata.Length); if (DataReceived != null) { DataReceived.BeginInvoke(client.Name, recdata, null, null);//异步输出数据 } ns.BeginRead(client.buffer, 0, client.buffer.Length, new AsyncCallback(TCPCallBack), client); } else { client.DisConnect(); lstClient.Remove(client); BindLstClient(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); client.DisConnect(); lstClient.Remove(client); BindLstClient(); } } }
/// <summary> /// 连接新的服务端 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnConn_Click(object sender, EventArgs e) { LeafTCPClient client = new LeafTCPClient(); try { client.NetWork = new TcpClient(); client.NetWork.Connect(txtServerIP.Text.Trim(), (int)nmServerPort.Value);//连接服务端 client.SetName(); client.NetWork.GetStream().BeginRead(client.buffer, 0, client.buffer.Length, new AsyncCallback(TCPCallBack), client); lstClient.Add(client); BindLstClient(); LogHelper.WriteLog("TCP Client Connect: " + txtServerIP.Text); } catch (Exception ex) { client.DisConnect(); if (LanguageSet.Language == "0") { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
/// <summary> /// 客户端通讯回调函数 /// </summary> /// <param name="ar"></param> private void TCPCallBack(IAsyncResult ar) { LeafTCPClient client = (LeafTCPClient)ar.AsyncState; if (client.NetWork.Connected) { NetworkStream ns = client.NetWork.GetStream(); byte[] recdata = new byte[ns.EndRead(ar)]; if (recdata.Length > 0) { Array.Copy(client.buffer, recdata, recdata.Length); if (DataReceived != null) { DataReceived.BeginInvoke(client.Name, recdata, null, null);//异步输出数据 } ns.BeginRead(client.buffer, 0, client.buffer.Length, new AsyncCallback(TCPCallBack), client); } else { client.DisConnect(); lstClient.Remove(client); BindLstClient(); } } }
/// <summary> /// 回调函数 /// </summary> /// <param name="ar"></param> private void TCPCallBack(IAsyncResult ar) { LeafTCPClient client = (LeafTCPClient)ar.AsyncState; if (client.NetWork.Connected) { NetworkStream ns = client.NetWork.GetStream(); try { byte[] recdata = new byte[ns.EndRead(ar)]; Array.Copy(client.buffer, recdata, recdata.Length); if (recdata.Length > 0) { if (DataReceived != null) { DataReceived.BeginInvoke(client.Name, recdata, null, null);//异步输出数据 } LogHelper.WriteLog("TCP Client Received: " + Encoding.Default.GetString(recdata)); ns.BeginRead(client.buffer, 0, client.buffer.Length, new AsyncCallback(TCPCallBack), client); } else { client.DisConnect(); lstClient.Remove(client); BindLstClient(); } } catch (Exception ex) { if (LanguageSet.Language == "0") { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } client.DisConnect(); lstClient.Remove(client); BindLstClient(); } } }
/// <summary> /// 回调函数 /// </summary> /// <param name="ar"></param> private void TCPCallBack(IAsyncResult ar) { LeafTCPClient client = (LeafTCPClient)ar.AsyncState; if (client.NetWork.Connected) { NetworkStream ns = client.NetWork.GetStream(); byte[] recdata = new byte[ns.EndRead(ar)]; Array.Copy(client.buffer, recdata, recdata.Length); if (recdata.Length > 0) { txtCmd.Invoke(new MethodInvoker(delegate { txtCmd.AppendText(Encoding.GetEncoding("gb2312").GetString(recdata)); })); ns.BeginRead(client.buffer, 0, client.buffer.Length, new AsyncCallback(TCPCallBack), client); } else { client.DisConnect(); } } }