Exemplo n.º 1
0
        private void BT_START_Click(object sender, RoutedEventArgs e)
        {
            _ip   = TB_AIPIP.Text.Trim();
            _port = int.Parse(TB_PORT.Text.ToString());

            if (_scm == null)
            {
                _scm = new SocketClientManager(1024 * 8);
                _scm.OnReceiveMsg   += ClientOnReceiveMsg;
                _scm.OnConnected    += ClientOnConnected;
                _scm.OnFaildConnect += ClientOnFailedConnect;
                _scm.OnDisconnect   += ClientOnServerDisconnect;
            }
            else
            {
                _scm.Stop();
            }
            _scm.Start(_ip, _port);
        }
Exemplo n.º 2
0
        public void Start(int timer, string ip, int port)
        {
            try
            {
                AppendToListView("Socket " + _msgClass4Log + " : Start ! IP >>" + ip + ":" + port.ToString(), CONVERTOR, MES, "OK");
                //初始化变量
                _PING_Send     = false;
                _PING_Received = false;

                //判断赋值
                if (_timer != timer || _ip != ip || _port != port)
                {
                    _ip    = ip;
                    _port  = port;
                    _timer = timer;
                }

                //开启Socket端口
                try
                {
                    if (_scm != null)
                    {
                        _scm.Stop();
                    }
                    else
                    {
                        _scm = new SocketClientManager(1024 * 8);
                        _scm.OnReceiveMsg   += ClientOnReceiveMsg;
                        _scm.OnConnected    += ClientOnConnected;
                        _scm.OnFaildConnect += ClientOnFailedConnect;
                        _scm.OnDisconnect   += ClientOnServerDisconnect;
                    }

                    _scm.Start(_ip, _port);
                }
                catch
                {
                    AppendToListView("Socket " + _msgClass4Log + " : SOCKETBASE_0001", "SOCKETBASE", ERROR, "NG");
                    _scm.Stop();
                }

                //开启主线程
                if (_mainThread == null)
                {
                    _mainThread      = new Thread(MainSocketThread);
                    _mainThread_isOn = true;
                    _mainThread.Start();
                }
                else
                {
                    //线程不在运行或者线程已经被Aborted
                    if (_mainThread.ThreadState != ThreadState.Running || _mainThread.ThreadState == ThreadState.Aborted)
                    {
                        //开始主线程
                        _mainThread      = new Thread(MainSocketThread);
                        _mainThread_isOn = true;
                        _mainThread.Start();
                    }//如果线程正在运行
                    else if (_mainThread.ThreadState == ThreadState.Running)
                    {
                        return;
                    }
                    else
                    {
                    }
                }
            }
            catch
            {
                AppendToListView("Socket " + _msgClass4Log + " : SOCKETBASE_0002", "SOCKETBASE", ERROR, "NG");
            }
        }