Ejemplo n.º 1
0
        /// <summary>
        /// 异步等待连接返回结果
        /// </summary>
        /// <param name="port"></param>
        /// <param name="ip"></param>
        /// <returns></returns>
        public bool ConnectTo(int port, string ip)
        {
            try
            {
                if (!IsClose())
                {
                    Close();
                }

                isConnected           = false;
                _channelProviderState = ChannelProviderType.AsyncWait;

                using (new LockWait(ref _lockParam))
                {
                    CreatedConnectToBindArgs(port, ip);
                }
                _mReset.WaitOne(connectioTimeout);
                isConnected = socket.Connected;

                return(isConnected);
            }
            catch (Exception)
            {
                Close();
                throw;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 异步等待连接返回结果
        /// </summary>
        /// <param name="port"></param>
        /// <param name="ip"></param>
        /// <returns></returns>
        public bool ConnectTo(int port, string ip)
        {
            try
            {
                if (isConnected || cliSocket != null)
                {
                    Close();
                }

                isConnected          = false;
                channelProviderState = ChannelProviderType.AsyncWait;

                IPEndPoint ips = new IPEndPoint(IPAddress.Parse(ip), port);

                cliSocket = CreateConnectAsync(ips, cliRecBuffer);

                mReset.WaitOne(cliConnectTimeout);
                isConnected = cliSocket.Connected;

                if (isConnected)
                {
                    InitializePool(cliConSend);
                }

                return(isConnected);
            }
            catch (Exception ex)
            {
                cliSocket.Dispose();
                throw ex;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 同步连接
        /// </summary>
        /// <param name="port"></param>
        /// <param name="ip"></param>
        /// <returns></returns>
        public bool ConnectSync(int port, string ip)
        {
            if (isConnected || cliSocket != null)
            {
                Close();
            }

            isConnected          = false;
            channelProviderState = ChannelProviderType.Sync;
            int retry = 3;

            IPEndPoint ips = new IPEndPoint(IPAddress.Parse(ip), port);

            cliSocket = new Socket(ips.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
            {
                LingerState = new LingerOption(true, 0),
                NoDelay     = true
            };

            while (retry > 0)
            {
                try
                {
                    --retry;
                    cliSocket.Connect(ips);
                    isConnected = true;
                    return(true);
                }
                catch (Exception ex)
                {
                    Close();
                    if (retry <= 0)
                    {
                        throw ex;
                    }
                    Thread.Sleep(1000);
                }
            }
            return(false);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 同步连接
        /// </summary>
        /// <param name="port"></param>
        /// <param name="ip"></param>
        /// <returns></returns>
        public bool ConnectSync(int port, string ip)
        {
            if (!IsClose())
            {
                Close();
            }

            isConnected          = false;
            channelProviderState = ChannelProviderType.Sync;
            int retry = 3;

            CreateTcpSocket(port, ip);

            //using (LockWait lwait = new LockWait(ref lParam))
            //{
            //    CreatedConnectToBindArgs(port,ip);
            //}
            while (retry > 0)
            {
                try
                {
                    --retry;
                    socket.Connect(ipEndPoint);
                    isConnected = true;
                    return(true);
                }
                catch (Exception ex)
                {
                    Close();
                    if (retry <= 0)
                    {
                        throw ex;
                    }
                    Thread.Sleep(1000);
                }
            }
            return(false);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 异步建立连接
        /// </summary>
        /// <param name="port"></param>
        /// <param name="ip"></param>
        public void Connect(int port, string ip)
        {
            try
            {
                if (!IsClose())
                {
                    Close();
                }

                isConnected           = false;
                _channelProviderState = ChannelProviderType.Async;

                using (new LockWait(ref _lockParam))
                {
                    CreatedConnectToBindArgs(port, ip);
                }
            }
            catch (Exception)
            {
                Close();
                throw;
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 异步建立连接
        /// </summary>
        /// <param name="port"></param>
        /// <param name="ip"></param>
        public void Connect(int port, string ip)
        {
            try
            {
                if (isConnected || cliSocket != null)
                {
                    Close();
                }

                isConnected          = false;
                channelProviderState = ChannelProviderType.Async;
                InitializePool(cliConSend);

                IPEndPoint ips = new IPEndPoint(IPAddress.Parse(ip), port);

                cliSocket = CreateConnectAsync(ips, cliRecBuffer);
            }
            catch (Exception ex)
            {
                Close();
                throw ex;
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 同步连接
        /// </summary>
        /// <param name="port"></param>
        /// <param name="ip"></param>
        /// <returns></returns>
        public bool ConnectSync(int port, string ip)
        {
            if (!IsClose())
            {
                Close();
            }

            isConnected           = false;
            _channelProviderState = ChannelProviderType.Sync;
            var retry = 3;

            using (new LockWait(ref _lockParam))
            {
                CreatedConnectToBindArgs(port, ip);
            }
            while (retry > 0)
            {
                try
                {
                    --retry;
                    socket.Connect(ipEndPoint);
                    isConnected = true;
                    return(true);
                }
                catch (Exception)
                {
                    Close();
                    if (retry <= 0)
                    {
                        throw;
                    }
                    Thread.Sleep(1000);
                }
            }
            return(false);
        }