Example #1
0
        public Result Connect(Commor commor)
        {
            commor.Connector = null;
            var ethernetCommor = (EthernetCommor)commor.Communicator;
            var socket         = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                ConnectSocketDelegate connect     = ConnectSocket;
                IAsyncResult          asyncResult = connect.BeginInvoke(socket, ethernetCommor.IP, ethernetCommor.Port, null, null);
                bool success = asyncResult.AsyncWaitHandle.WaitOne(1000, false);
                if (!success)
                {
                    return(new Result(string.Format("连接{0}失败({1} :{2})", ethernetCommor.Name, ethernetCommor.IP, ethernetCommor.Port)));
                }
                //socket.Connect(ethernetCommor.IP, ethernetCommor.Port); 连接失败时卡死 超时(20s)
            }
            catch (Exception ex)
            {
                return(new Result(ex.Message));
            }
            if (!socket.Connected)
            {
                return(new Result(string.Format("连接失败:{0}:{1}", ethernetCommor.IP, ethernetCommor.Port)));
            }
            commor.Connector = socket;
            return(Result.Success);
        }
Example #2
0
        /// <summary>
        /// 启动
        /// </summary>
        /// <returns></returns>
        public bool Start()
        {
            try
            {
                //先执行ping 命令
                KeepServer = true;
                IPAddress  ip   = IPAddress.Parse(ComPara.ServerIP);
                IPEndPoint ipep = new IPEndPoint(ip, ComPara.Port);//IP和端口

                //Tcpsocket.Connect(new IPEndPoint(ip, ComPara.Port));
                ConnectSocketDelegate connect     = ConnectSocket;
                IAsyncResult          asyncResult = connect.BeginInvoke(ipep, Tcpsocket, null, null);

                bool connectSuccess = asyncResult.AsyncWaitHandle.WaitOne(3 * 1000, false);
                if (!connectSuccess)
                {
                    //MessageBox.Show(string.Format("失败!错误信息:{0}", "连接超时"));
                    return(false);
                }

                bool exmessage = connect.EndInvoke(asyncResult);
                if (exmessage == false)
                {
                    return(false);
                }
                //if (!string.IsNullOrEmpty(exmessage))
                //{
                //    //MessageBox.Show(string.Format("失败!错误信息:{0}", exmessage));
                //    return false;
                //}
                LastRecTime = DateTime.Now;

                processor = new Thread(Communication);
                processor.IsBackground = true;
                processor.Start();

                RecevProvessor = new Thread(ReceverMes);
                RecevProvessor.IsBackground = true;
                RecevProvessor.Start();
                return(true);
            }
            catch (Exception ex)
            {
                KeepServer = false;
                IODeviceInfo IOInfo = new IODeviceInfo();
                IOInfo.ID           = this.DeviceID;
                IOInfo.bIsCommBreak = true;
                DelegateState.InvokeIOFeedBackEvent(IOInfo);
                return(false);
            }
            finally
            {
                this.communicationobserve_timer.Enabled = true;
            }
        }