예제 #1
0
        /// <summary>
        /// 重新连接方法
        /// </summary>
        internal void Reconnect()
        {
            string temp = string.Format("TCP主机地址:{0},端口号:{1}", ServerIp, ServerPort); //TCP连接的主机地址与端口

            LastErrorMessage = "TCP连接意外断开,正在尝试重连。" + temp;
            //FileClient.WriteFailureInfo(LastErrorMessage);
            try
            {
                BaseClient.Close();
                BaseClient = HoldLocalPort ? new TcpClient(LocalEndPoint) : new TcpClient();
                BaseClient.Connect(ServerIp, ServerPort);
                SetName();

                //重连次数+1,同时调用事件委托
                ReconnTimer++;
                if (ReconnTimerChanged != null)
                {
                    ReconnTimerChanged.BeginInvoke(Name, ReconnTimer, null, null);
                }

                NetStream = BaseClient.GetStream();
                if (AutoReceive)
                {
                    NetStream.BeginRead(Buffer, 0, Buffer.Length, new AsyncCallback(TcpCallBack), this);
                }
                //FileClient.WriteFailureInfo("TCP重新连接成功。" + temp);
            }
            //假如出现异常,将错误信息写入日志并进入下一次循环
            catch (Exception e)
            {
                LastErrorMessage = string.Format("TCP重新连接失败:{0}。", e.Message) + temp;
            }
        }
예제 #2
0
        /// <summary>
        /// UDP重新连接方法
        /// </summary>
        private void TcpAutoReconnect()
        {
            while (true)
            {
                Thread.Sleep(1000);

                //假如属性提示未连接,则UDP重连线程挂起
                if (!IsConnected)
                {
                    Auto_UdpReconnect.WaitOne();
                }
                //假如属性提示已连接但实际上连接已断开,尝试重连
                //else if (IsConnected && !IsSocketConnected())
                else if (IsConnected && !IsConnected_Socket)
                {
                    string temp = string.Format("UDP主机地址:{0},端口号:{1}", ServerIp, ServerPort); //UDP连接的主机地址与端口
                    LastErrorMessage = "UDP连接意外断开,正在尝试重连。" + temp;
                    FileClient.WriteFailureInfo(LastErrorMessage);
                    try
                    {
                        BaseClient.Close();
                        BaseClient = HoldLocalPort ? new UdpClient(LocalEndPoint) : new UdpClient();
                        BaseClient.Connect(ServerIp, ServerPort);
                        SetName();

                        //重连次数+1,同时调用事件委托
                        ReconnTimer++;
                        if (ReconnTimerChanged != null)
                        {
                            ReconnTimerChanged.BeginInvoke(Name, ReconnTimer, null, null);
                        }
                        if (Connected != null)
                        {
                            Connected.BeginInvoke(Name, new EventArgs(), null, null);
                        }

                        //NetStream = BaseClient.GetStream();
                        if (AutoReceive)
                        {
                            BaseClient.BeginReceive(new AsyncCallback(ReceiveCallBack), this);
                        }
                        //NetStream.BeginRead(Buffer, 0, Buffer.Length, new AsyncCallback(TcpCallBack), this);
                        //IsSocketConnected();
                        FileClient.WriteFailureInfo("UDP重新连接成功。" + temp);
                    }
                    //假如出现异常,将错误信息写入日志并进入下一次循环
                    catch (Exception e)
                    {
                        LastErrorMessage = string.Format("UDP重新连接失败:{0}。", e.Message) + temp;
                        FileClient.WriteExceptionInfo(e, LastErrorMessage, false);
                        continue;
                        //TODO: 是否抛出异常?
                        //throw; //假如不需要抛出异常,注释此句
                    }
                }
            }
        }
예제 #3
0
        /// <summary>
        /// 利用特定的本地端口与TCP服务端连接
        /// </summary>
        /// <param name="server">TCP服务端IP</param>
        /// <param name="port">端口号</param>
        /// <param name="localIp">本地IP</param>
        /// <param name="localPort">指定的本地端口(假如小于等于0则随机)</param>
        /// <returns>假如建立连接成功,返回1,否则返回0</returns>
        public int Connect(string server, int port, string localIp, int localPort)
        {
            //尝试建立连接
            int result = 1;

            try
            {
                ServerIp   = server;
                ServerPort = port;
                //BaseClient = new TcpClient(ServerIp, ServerPort);
                BaseClient = !string.IsNullOrWhiteSpace(localIp) && localPort > 0 ? new TcpClient(new IPEndPoint(IPAddress.Parse(localIp), localPort)) : new TcpClient();
                BaseClient.Connect(ServerIp, ServerPort);
                SetName(); //修改连接名称

                //重置重连次数,同时调用事件委托
                ReconnTimer = 0;
                if (ReconnTimerChanged != null)
                {
                    ReconnTimerChanged.BeginInvoke(Name, ReconnTimer, null, null);
                }

                BaseClient.ReceiveBufferSize = ReceiveBufferSize; //接收缓冲区的大小
                NetStream = BaseClient.GetStream();               //发送与接收数据的数据流对象
                raiser.Run();
            }
            catch (Exception e)
            {
                LastErrorMessage = string.Format("无法建立TCP连接,IP{0},端口号{1}:{2}", ServerIp, ServerPort, e.Message);
                if (logging)
                {
                    FileClient.WriteExceptionInfo(e, LastErrorMessage, false);
                }
                Close();
                result = 0;
                throw; //假如不需要抛出异常,注释此行
            }

            IsConnected = BaseClient.Connected;
            IsSocketConnected();
            if (IsConnected)
            {
                //调用Tcp连接事件委托
                if (Connected != null)
                {
                    Connected.BeginInvoke(Name, (new EventArgs()), null, null);
                }
                if (Thread_TcpReconnect == null)
                {
                    Auto_TcpReconnect   = new AutoResetEvent(true);
                    Thread_TcpReconnect = new Thread(new ThreadStart(TcpAutoReconnect))
                    {
                        IsBackground = true
                    };
                    //Thread_TcpReconnect.IsBackground = true;
                    Thread_TcpReconnect.Start();
                }
                else
                {
                    Auto_TcpReconnect.Set();
                }
                if (AutoReceive)
                {
                    NetStream.BeginRead(Buffer, 0, Buffer.Length, new AsyncCallback(TcpCallBack), this);
                }
            }
            return(result);
        }