コード例 #1
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            int heigth = this.Height;

            UtilityClass.hide_show(this, ref heigth, timer1);

            if (HbClient != null && HbClient.Client != null && HbClient.Connected && Hbstream != null)
            {
                var command = BitConverter.GetBytes(0xFE01);
                try
                {
                    Hbstream.Write(command, 0, command.Length);
                }
                catch (Exception)
                {
                    HbClient.Close();
                }
            }
        }
コード例 #2
0
        private void connect(IPAddress ipaddr, BackgroundWorker MyWorker, DoWorkEventArgs e)
        {
            if (MyWorker.CancellationPending)
            {
                e.Cancel = true;
            }

            string[] cportstr = { "命令端口" };
            int      cport    = Int16.Parse(XmlHelper.GetConfigValue(xmldoc, cportstr));

            string[] dportstr = { "数据端口" };
            int      dport    = Int16.Parse(XmlHelper.GetConfigValue(xmldoc, dportstr));

            try
            {
                AddtoBox(Color.Black, "连接命令端口……\r\n");

                Tclient.BeginConnect(ipaddr, cport, new AsyncCallback(ConnnectCallBack), Tclient);
                while (true)
                {
                    Thread.Sleep(50);
                    if (MyWorker.CancellationPending == false)
                    {
                        if ((Tclient.Client != null) && (Tclient.Connected == true))
                        {
                            Tstream = Tclient.GetStream();
                            AddtoBox(Color.Black, "命令端口已连接。\r\n");
                            break;
                        }
                    }
                    else
                    {
                        e.Cancel = true;
                        return;
                    }
                }
            }
            catch (SocketException myEx)
            {
                e.Result = myEx.ErrorCode;
                bConnect = false;
                AddtoBox(Color.Black, "命令端口连接失败。\r\n");
                return;
            }
            try
            {
                AddtoBox(Color.Black, "数据命令端口……\r\n");

                Dclient.BeginConnect(ipaddr, dport, new AsyncCallback(ConnnectCallBack), Dclient);
                while (true)
                {
                    Thread.Sleep(50);
                    if (MyWorker.CancellationPending == false)
                    {
                        if ((Dclient.Client != null) && (Dclient.Connected == true))
                        {
                            Dstream = Dclient.GetStream();
                            AddtoBox(Color.Black, "数据端口已连接。\r\n");
                            break;
                        }
                    }
                    else
                    {
                        e.Cancel = true;
                        return;
                    }
                }
                bConnect = true;
                //AddtoBox(Color.Black, "数据端口已连接。\r\n");
            }
            catch (SocketException myEx)
            {
                e.Result = myEx.ErrorCode;
                bConnect = false;
                AddtoBox(Color.Black, "数据端口连接失败。\r\n");
                return;
            }
            HbClient.BeginConnect(IPAddress.Parse("127.0.0.1"), 32100, new AsyncCallback(HBConnnectCallBack), HbClient);
            int count = 0;

            while (true)
            {
                count++;
                Thread.Sleep(50);
                if (MyWorker.CancellationPending == false)
                {
                    if ((HbClient.Client != null) && (HbClient.Connected == true))
                    {
                        Hbstream = HbClient.GetStream();
                        AddtoBox(Color.Black, "HB端口已连接。\r\n");
                        break;
                    }
                }
                else
                {
                    //e.Cancel = true;
                    return;
                }
                if (count > 100)//5s
                {
                    break;
                }
            }
        }