/// <summary>
        /// 构造函数。
        /// </summary>
        /// <param name="sci"></param>
        /// <param name="userInfo"></param>
        /// <param name="ports"></param>
        public HostListenService(StartClassInfo sci,UserInfo userInfo, PortSettings ports)
        {
            this.sci = sci;
            this.userInfo = userInfo;
            this.ports = ports;

            this.udpSocket = new UDPSocket(this.ports.ClientCallback);
            this.udpSocket.Changed += this.RaiseChanged;
            this.udpSocket.DataArrival += new SocketDataArrivalEventHandler<Msg>(delegate(Msg sender)
            {
                try
                {
                    Thread thread = new Thread(new ThreadStart(delegate()
                    {
                        this.ReceiveClientData(sender);
                    }));
                    thread.IsBackground = true;
                    thread.Start();
                }
                catch (Exception e)
                {
                    UtilTools.OnExceptionRecord(e, typeof(UDPSocket));
                }
            });
            this.workUpTcpService = new WorkUpTcpService(this.ports.FileUpTransfer);
            this.workUpTcpService.Changed += this.RaiseChanged;
            this.workUpTcpService.DataArrival += new SocketDataArrivalEventHandler<FileMSG>(delegate(FileMSG sender)
            {
                this.ReceiveClientData(sender as UploadFileMSG);
            });
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sci"></param>
        /// <param name="hostAddress"></param>
        /// <param name="ports"></param>
        public void Start(StartClassInfo sci, HostAddress hostAddress, PortSettings ports)
        {
            try
            {
                if (sci == null || ports == null || hostAddress == null) return;
                int broadcastPort = ports.HostBroadcast;

                Thread thread = HostBroadcastService.PortThreadCache[broadcastPort] as Thread;

                #region 如果当前广播未关闭,强制关闭。
                if (thread != null)
                {
                    try
                    {
                        this.Stop();
                        thread.Abort();
                    }
                    finally
                    {
                        thread = null;
                    }
                }
                #endregion

                this.RaiseChanged("开启主机广播...");

                IPEndPoint broadcastAddr = new IPEndPoint(hostAddress.BroadcastAddress, broadcastPort);

                HostBroadcast data = new HostBroadcast();
                data.SName = string.Format("{0}[{1}]{2}({3})", this.info.UserName, sci.ClassInfo.ClassName, sci.CatalogInfo.CatalogName, sci.CatalogInfo.TypeName);
                data.Ports = ports;
                data.UID = this.info.UserID;
                data.Time = DateTime.Now;

                int interval = ports.BroadcastInterval;

                thread = new Thread(new ThreadStart(delegate()
                {
                    this.isStart = true;
                    //主机广播。
                    this.sendHostBroadcast(data, broadcastAddr, interval);
                    //发送主机关闭广播。
                    this.sendHostCloseBroadcast(broadcastAddr);
                    //移除缓存。
                    HostBroadcastService.PortThreadCache[broadcastPort] = null;
                }));
                thread.IsBackground = true;
                HostBroadcastService.PortThreadCache[broadcastPort] = thread;
                thread.Start();
            }
            catch (Exception x)
            {
                this.OnExceptionRecord(x, this.GetType());
                this.RaiseChanged(x.Message);
                MessageBox.Show(x.Message, "循环广播时发生异常:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 /// <summary>
 /// 构造函数。
 /// </summary>
 private HostNetService(ICoreService service, StartClassInfo sci)
 {
     this.info = service["userinfo"] as UserInfo;
     this.ports = service["portsettings"] as PortSettings;
     this.hostAddress = service["hostaddress"] as HostAddress;
     //主机广播。
     this.hostBroadcast = new HostBroadcastService(info);
     this.hostBroadcast.Changed += this.OnRaiseChanged;
     //主机侦听。
     this.hostListenService = new HostListenService(this.sci = sci, this.info, this.ports);
     this.hostListenService.Changed += this.OnRaiseChanged;
     this.hostListenService.UpdateControls += this.OnUpdateControls;
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show(string.Format("您确认{0}上课?", this.start ? "结束" : "开始"), "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) != DialogResult.Yes)
                {
                    return;
                }
                this.OnClearErrorEvent();
                StartClassInfo sci = new StartClassInfo();
                Grade g = this.ddlGrade.SelectedItem as Grade;
                if (g != null)
                {
                    sci.GradeID = g.GradeID;
                    sci.GradeName = g.GradeName;
                    sci.Evaluate = g.Evaluate;
                }
                sci.ClassInfo = this.ddlClass.SelectedItem as Class;
                sci.CatalogInfo = this.ddlCatalog.SelectedItem as Catalog;
                sci.LoginMethod = (EnumLoginMethod)int.Parse(this.ddlLoginMethod.SelectedValue.ToString());
                sci.Password = this.txtLoginPassword.Text.Trim();

                if ((sci.LoginMethod == EnumLoginMethod.Password) && string.IsNullOrEmpty(sci.Password))
                {
                    string err = "请设置指定密码!";
                    this.OnMessageEvent(MessageType.Normal | MessageType.PopupInfo, err);
                    this.OnSetErrorEvent(this.txtLoginPassword, err);
                    this.txtLoginPassword.Focus();
                    return;
                }
                this.WriteStatusInfo(this.start = !this.start);
                this.btnStart_MouseLeave(sender, e);

                this.CoreService["startclassinfo"] = sci;

                this.OnCrossPluginSend(new CrossPluginEventArgs(DockStyle.Right, new object[] { "syncdata", !this.start }));
                this.OnCrossPluginSend(new CrossPluginEventArgs(DockStyle.Fill, new object[] { "load", this.start, sci }));
            }
            catch (Exception x)
            {
                this.OnMessageEvent(MessageType.PopupWarn, "发生异常:\r\n" + x.Message);
            }
        }
 private HostNetService initHostNetService(StartClassInfo sci)
 {
     this.service["startclassinfo"] = sci;
     HostNetService hostNetService = HostNetService.Instance(this.service);
     if (hostNetService != null)
     {
         hostNetService.RaiseChanged += this.onRaiseChanged;
         hostNetService.UpdateControls += this.onUpdateControls;
     }
     return hostNetService;
 }