Exemplo n.º 1
0
        /// <summary>
        /// 客户端通讯回调函数
        /// </summary>
        /// <param name="ar"></param>
        private void TCPCallBack(IAsyncResult ar)
        {
            LeafTCPClient client = (LeafTCPClient)ar.AsyncState;
            int           i      = 0;

            if (client.NetWork.Connected)
            {
                NetworkStream ns = client.NetWork.GetStream();
                try
                {
                    i = ns.EndRead(ar);
                }
                catch (IOException ex)
                {
                    MessageBox.Show("" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                byte[] recdata = new byte[i];
                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();
                }
            }
        }
Exemplo n.º 2
0
        /// <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);
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// 对当前选中的客户端发送数据
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="data"></param>
 public bool SendData(byte[] data)
 {
     if (lstConn.SelectedItems.Count > 0)
     {
         for (int i = 0; i < lstConn.SelectedItems.Count; i++)
         {
             LeafTCPClient selClient = (LeafTCPClient)lstConn.SelectedItems[i];
             try
             {
                 selClient.NetWork.GetStream().Write(data, 0, data.Length);
             }
             catch (Exception ex)
             {
                 MessageBox.Show(selClient.Name + ":" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 return(false);
             }
         }
         return(true);
     }
     else
     {
         MessageBox.Show("无可用客户端", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return(false);
     }
 }
Exemplo n.º 4
0
        /// <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();
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 客户端连接初始化
        /// </summary>
        /// <param name="o"></param>
        private void Acceptor(IAsyncResult o)
        {
            TcpListener server = o.AsyncState as TcpListener;

            try
            {
                //初始化连接的客户端
                LeafTCPClient newClient = new LeafTCPClient();
                newClient.NetWork = server.EndAcceptTcpClient(o);
                lstClient.Add(newClient);
                BindLstClient();
                newClient.NetWork.GetStream().BeginRead(newClient.buffer, 0, newClient.buffer.Length, new AsyncCallback(TCPCallBack), newClient);
                server.BeginAcceptTcpClient(new AsyncCallback(Acceptor), server);//继续监听客户端连接
            }
            catch (ObjectDisposedException ex)
            { //监听被关闭
            }
            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);
                }
            }
        }
Exemplo n.º 6
0
        /// <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);//异步输出数据
                        }

                        LogHelper.WriteLog("TCP Server 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();
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// 对当前选中的客户端发送数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="data"></param>
        public bool SendData(byte[] data)
        {
            if (lstConn.SelectedItems.Count > 0)
            {
                for (int i = 0; i < lstConn.SelectedItems.Count; i++)
                {
                    LeafTCPClient selClient = (LeafTCPClient)lstConn.SelectedItems[i];
                    try
                    {
                        selClient.NetWork.GetStream().Write(data, 0, data.Length);


                        LogHelper.WriteLog("TCP Server Send date:" + Encoding.Default.GetString(data));
                    }
                    catch (Exception ex)
                    {
                        if (LanguageSet.Language == "0")
                        {
                            MessageBox.Show(selClient.Name + ":" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        else
                        {
                            MessageBox.Show(selClient.Name + ":" + ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        return(false);
                    }
                }
                return(true);
            }
            else
            {
                if (LanguageSet.Language == "0")
                {
                    MessageBox.Show("无可用客户端", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("No client available", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                return(false);
            }
        }
Exemplo n.º 8
0
        /// <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();
                }
            }
        }