예제 #1
0
        private void cmdStop_Click(object sender, EventArgs e)
        {
            //UdpServer m_UdpServer = new UdpServer();

            //m_UdpServer.StartListen();

            try
            {
                if (m_Protocol == null || m_Comm == null)
                {
                    MessageBox.Show("当前未连接!");
                    return;
                }

                m_Protocol.Main_Stop();

                m_Comm.StopClient();

                m_Protocol = null;
                m_Comm     = null;

                MessageBox.Show("已断开连接!");
            }
            catch
            {
            }
        }
예제 #2
0
        private void cmdStart_Click(object sender, EventArgs e)
        {
            if (!HyNetUtility.IsValidIP(this.txtIP.Text))
            {
                MessageBox.Show("设定的IP地址不正确!");
                this.txtIP.Focus();
                return;
            }

            int port;

            if (!int.TryParse(this.txtPort.Text, out port) || port <= 0 || port > 65535)
            {
                MessageBox.Show("设定的端口不正确!");
                this.txtPort.Focus();
                return;
            }

            if (m_Comm != null && m_Comm.Connected)
            {
                MessageBox.Show("当前已连接!");
                return;
            }


            byte[] autosendData = null;
            if (chkHex.Checked)
            {
                autosendData = HexEncoding.Instance.GetBytes(this.TextBoxSend.Text);
            }
            else
            {
                //用GB2312编码
                autosendData = Encoding.GetEncoding("gb2312").GetBytes(this.TextBoxSend.Text);
            }
            m_Protocol = new ByteStreamProtocol((int)updTimer.Value, checkBoxAuto.Checked, autosendData, false);

            m_Comm            = new TCPClient(this.txtIP.Text, port, CommConst.BUFFERSIZE, m_Protocol);
            m_Protocol.m_Comm = m_Comm;

            this.commFrameShow1.Init(m_Protocol);

            //开始尝试连接
            m_Protocol.ClearCache();
            int StartTick = HyTick.TickTimeGet();

            m_Comm.StartClient();
            while (!m_Comm.Connected)
            {
                //等待10秒是否连接成功
                if (HyTick.TickTimeIsArrived(StartTick, 10000))
                {
                    MessageBox.Show("连接失败!");
                    return;
                }
                Application.DoEvents();
            }

            MessageBox.Show("连接成功!");

            m_Protocol.Main_Start();
        }