Exemplo n.º 1
0
 /// <summary>
 /// 发送数据
 /// </summary>
 /// <param name="data"></param>
 /// <returns></returns>
 public bool SendData(byte[] data)
 {
     if (UDPClient_Connect_listBox.SelectedItems.Count > 0)
     {
         for (int i = 0; i < UDPClient_Connect_listBox.SelectedItems.Count; i++)
         {
             try
             {
                 ZXBC_UDPClient client = (ZXBC_UDPClient)UDPClient_Connect_listBox.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);
     }
 }
Exemplo n.º 2
0
 private void UDPClient_add_button_Click(object sender, EventArgs e)
 {
     try
     {
         ZXBC_UDPClient client = new ZXBC_UDPClient();
         client.NetWork = new UdpClient();
         client.NetWork.Connect(UDPClient_IP_textBox.Text.Trim(), (int)UDPClient_port_numericUpDown.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();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// 接收到数据
        /// </summary>
        /// <param name="ar"></param>
        public void ReceiveCallback(IAsyncResult ar)
        {
            ZXBC_UDPClient uclient = (ZXBC_UDPClient)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);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 接收到数据
        /// </summary>
        /// <param name="ar"></param>
        public void ReceiveCallback(IAsyncResult ar)
        {
            ZXBC_UDPClient userver  = (ZXBC_UDPClient)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);//继续异步接收数据
                }
            }
        }