Exemplo n.º 1
0
 public MyDbgHandler(displayMessageDelegate method, string prefix)
 {
     displayMessageMethod = method;
     this.prefix          = prefix;
 }
Exemplo n.º 2
0
 public MainWindow()
 {
     InitializeComponent();
     displayMessage = new displayMessageDelegate(display);
 }
Exemplo n.º 3
0
 public MyDbgHandler(displayMessageDelegate method, string prefix)
 {
     displayMessageMethod = method;
     this.prefix = prefix;
 }
Exemplo n.º 4
0
        private void udpListen()//监听,线程的实际代码
        {
            #region 建立服务器
            int i = 1;
            while (true)//循环,端口不可用自动加1
            {
                try
                {
                    this.Invoke(new Action(() =>//从子线程中访问主界面控件值
                    {
                        IPEndPoint LocalIPEndPoint = new IPEndPoint(IPAddress.Parse(comboBoxHostIp.Text), int.Parse(textBoxPortNumber.Text));
                        //udpserver = new UdpClient(int.Parse(this.textBoxPortNumber.Text));//创建UDP服务器,绑定要监听的端口
                        UDPServer = new UdpClient(LocalIPEndPoint);//创建UDP服务器,绑定要监听的端口
                    }));
                    break;
                }
                catch (Exception ex)
                {
                    //端口不可用就将预设的端口号加1
                    MessageBox.Show(ex.Message, "错误,该端口号不能使用,点击确定按钮自动加一尝试重新开启", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.Invoke(new Action(() =>
                    {
                        this.textBoxPortNumber.Text = (int.Parse(this.textBoxPortNumber.Text) + i++).ToString();
                    }));
                }
            }
            #endregion

            remoteIpAndPort = new IPEndPoint(IPAddress.Any, 0);//定义IPENDPOINT,装载远程IP地址和端口

            while (true)
            {
                try
                {
                    //将udpserver接受到指定的远程主机的数据包分别转换成字符串和数组形式保存在临时变量
                    receivedStr  = System.Text.Encoding.UTF8.GetString(UDPServer.Receive(ref remoteIpAndPort));
                    receiveddata = UDPServer.Receive(ref remoteIpAndPort);

                    if (DataCheckEvent != null)                                   //事件调用
                    {                                                             //如果有对象注册
                        if (DataCheckEvent(receiveddata, receiveddata.Length))    //和校验通过
                        {
                            DataanalysisEvent(receiveddata, receiveddata.Length); //数据解析
                        }
                    }

                    //定义匿名委托,分号结尾,用于刷新显示远程端口
                    displayMessageDelegate dis = delegate()
                    {
                        this.textBoxRemoteIp.Text   = remoteIpAndPort.Address.ToString(); //远程主机的IP显示到窗体
                        this.textBoxRemotePort.Text = remoteIpAndPort.Port.ToString();    //远程主机的端口号显示到窗体
                    };
                    this.Invoke(dis);                                                     //执行委托
                }

                catch (Exception e)
                {
                    MessageBox.Show(e.ToString(), "cuowu");
                    //break;
                }
            }
        }