예제 #1
0
        public bool Connect(string ip, int tcpport)
        {
            try
            {
                string    strException = string.Empty;
                IPAddress ipAddress    = IPAddress.Parse(ip);
                int       nPort        = Convert.ToInt32(tcpport);

                int nRet = reader.ConnectServer(ipAddress, nPort, out strException);
                if (nRet != 0)
                {
                    string strLog = "Connect reader failed, due to: " + strException;
                    Console.Write(strLog, 1);
                }
                else
                {
                    string strLog = "Reader connected  " + ipAddress.ToString() + "@" + nPort.ToString();
                    Console.Write(strLog, 0);
                    StackTrashParshedRecord();
                    reader.AnalyCallback = AnalyData;
                    return(true);
                }
                return(false);
            }
            catch (System.Exception ex)
            {
                Console.Write(ex.Message, 0);
                return(false);
            }
        }
예제 #2
0
        private void btnopenclient_Click(object sender, EventArgs e)
        {
            #region ipAddress和port用户输入判断
            IPAddress ipAddress = IPAddress.None;
            if (textBox2.Text == "") //判断是否为空
            {
                MessageBox.Show("对不起,Server IP输入不能为空!", "Error", MessageBoxButtons.OK);
            }
            else
            {
                try
                {
                    ipAddress = IPAddress.Parse(textBox2.Text);
                }
                catch
                {
                    MessageBox.Show("对不起,Server IP输入只能为IP地址!", "Error", MessageBoxButtons.OK);
                }
            }
            int port = 0;
            if (textBox1.Text == "") //判断是否为空
            {
                MessageBox.Show("对不起,Port输入不能为空!", "Error", MessageBoxButtons.OK);
            }
            else
            {
                try
                {
                    port = Convert.ToInt32(textBox1.Text);
                }
                catch
                {
                    MessageBox.Show("对不起,Port输入只能为数字!", "Error", MessageBoxButtons.OK);
                }
            }
            #endregion
            string strLog       = string.Empty;
            string strException = string.Empty;
            int    nRet         = readerServer.ConnectServer(ipAddress, port, out strException);
            if (nRet != 0)//如果连接服务端失败
            {
                strLog = "连接服务端失败,请确认是否已打开服务端,失败原因: " + strException;
            }
            else
            {
                strLog = "成功连接服务端" + ipAddress + ":" + port;
            }

            WriteLog(richTextBox1, strLog);
        }