/// <summary>
 /// 发送数据
 /// </summary>
 /// <param name="data"></param>
 /// <returns></returns>
 public bool SendData(byte[] data)
 {
     if (lstConn.SelectedItems.Count > 0)
     {
         for (int i = 0; i < lstConn.SelectedItems.Count; i++)
         {
             try
             {
                 LeafUDPClient client = (LeafUDPClient)lstConn.SelectedItems[i];
                 client.NetWork.Send(data, data.Length);
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 return(false);
             }
         }
         return(true);
     }
     else
     {
         MessageBox.Show("无可用客户端", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return(false);
     }
 }
Beispiel #2
0
        /// <summary>
        /// 接收到数据
        /// </summary>
        /// <param name="ar"></param>
        public void ReceiveCallback(IAsyncResult ar)
        {
            LeafUDPClient userver  = (LeafUDPClient)ar.AsyncState;
            string        ConnName = "";

            try
            {
                if (userver.NetWork.Client != null)
                {
                    IPEndPoint fclient = userver.ipLocalEndPoint;
                    Byte[]     recdata = userver.NetWork.EndReceive(ar, ref fclient);
                    ConnName = userver.ipLocalEndPoint.Port + "->" + fclient.ToString();
                    if (DataReceived != null)
                    {
                        DataReceived.BeginInvoke(ConnName, recdata, null, null);//异步输出数据
                    }
                    if (lstClient.Contains(ConnName) == false)
                    {//新的客户端
                        lstClient.Add(ConnName);
                        BindLstClient();
                    }
                }
            }
            catch (ObjectDisposedException ex) {}
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally {
                if (userver.NetWork.Client != null)
                {
                    userver.NetWork.BeginReceive(new AsyncCallback(ReceiveCallback), userver);//继续异步接收数据
                }
            }
        }
Beispiel #3
0
        private void btnConn_Click(object sender, EventArgs e)
        {
            try
            {
                LeafUDPClient client = new LeafUDPClient();
                client.NetWork = new UdpClient();
                client.NetWork.Connect(txtServerIP.Text.Trim(), (int)nmServerPort.Value);
                client.ipLocalEndPoint = (IPEndPoint)client.NetWork.Client.LocalEndPoint;
                client.Name            = client.ipLocalEndPoint.Port + "->" + client.NetWork.Client.RemoteEndPoint.ToString();
                client.NetWork.BeginReceive(new AsyncCallback(ReceiveCallback), client);//继续异步接收数据
                lstClient.Add(client);
                BindLstClient();

                LogHelper.WriteLog("UDP Add Connect: " + txtServerIP.Text);
            }
            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);
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// 接收到数据
        /// </summary>
        /// <param name="ar"></param>
        public void ReceiveCallback(IAsyncResult ar)
        {
            LeafUDPClient uclient = (LeafUDPClient)ar.AsyncState;

            try
            {
                if (uclient.NetWork.Client != null && uclient.NetWork.Client.Connected)
                {
                    IPEndPoint fclient  = uclient.ipLocalEndPoint;
                    Byte[]     recdata  = uclient.NetWork.EndReceive(ar, ref fclient);
                    string     ConnName = uclient.ipLocalEndPoint.Port + "->" + fclient.ToString();
                    if (DataReceived != null)
                    {
                        DataReceived.BeginInvoke(ConnName, recdata, null, null);//异步输出数据

                        LogHelper.WriteLog("UDP Receive Date: " + Encoding.Default.GetString(recdata));
                    }
                    uclient.NetWork.BeginReceive(new AsyncCallback(ReceiveCallback), uclient);//继续异步接收数据
                }
            }
            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);
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool SendData(byte[] data)
        {
            if (lstConn.SelectedItems.Count > 0)
            {
                for (int i = 0; i < lstConn.SelectedItems.Count; i++)
                {
                    try
                    {
                        LeafUDPClient client = (LeafUDPClient)lstConn.SelectedItems[i];
                        client.NetWork.Send(data, data.Length);

                        LogHelper.WriteLog("UDP Send Date: " + Encoding.Default.GetString(data));
                    }
                    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);
                        }
                        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);
            }
        }
        /// <summary>
        /// 接收到数据
        /// </summary>
        /// <param name="ar"></param>
        public void ReceiveCallback(IAsyncResult ar)
        {
            LeafUDPClient uclient = (LeafUDPClient)ar.AsyncState;

            try
            {
                if (uclient.NetWork.Client != null && uclient.NetWork.Client.Connected)
                {
                    IPEndPoint fclient  = uclient.ipLocalEndPoint;
                    Byte[]     recdata  = uclient.NetWork.EndReceive(ar, ref fclient);
                    string     ConnName = uclient.ipLocalEndPoint.Port + "->" + fclient.ToString();
                    if (DataReceived != null)
                    {
                        DataReceived.BeginInvoke(ConnName, recdata, null, null);               //异步输出数据
                    }
                    uclient.NetWork.BeginReceive(new AsyncCallback(ReceiveCallback), uclient); //继续异步接收数据
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }