Пример #1
0
        /// <summary>
        /// 소켓 다시 연결
        /// </summary>
        /// <param name="url"></param>
        /// <param name="port"></param>
        public void reconnect(string url, int port)
        {
            try
            {
                _sck.reconnectAttempt++;
                if (_sck.Connected == true)
                {
                    // 소켓이 연결이 되어있다면 소켓 연결 종료
                    _sck.Client.Shutdown(SocketShutdown.Both);
                    _sck.Client.Close();
                    _sck.Close();
                    _sck = null;
                }

                // 소캣 재설정
                _sck        = new StarBucksSocket();
                _sck.remote = url;
                _sck.port   = port;

                // Timeout 조절
                _sck.recentTime = DateTime.Now;
                _sck.finalTime  = DateTime.Now;

                // 서버로 다시 연결
                var result        = _sck.BeginConnect(url, port, null, null);
                var timeOutResult = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(3), false);
                if (!timeOutResult)
                {
                    connectEvent?.Invoke(this, new connectedArgs(_sck.remote, _sck.port, false));
                    return;
                }

                _sck.recentTime       = DateTime.Now;
                _sck.reconnectAttempt = 0;

                connectEvent?.Invoke(this, new connectedArgs(_sck.remote, _sck.port, true));
                ListenforMessage();
            }
            catch (Exception)
            {
            }
        }
Пример #2
0
        /// <summary>
        /// Socket 연결을 해제하고 Dispose 처리
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    // 관리 리소스 해지
                    if (_sck.Connected == true)
                    {
                        // 소캣 연결 해제
                        _sck.Client.Shutdown(SocketShutdown.Both);
                    }

                    _sck = null;
                    _sck = new StarBucksSocket();
                }

                this.disposed = true;
            }
        }
Пример #3
0
        public socket(string url, int port)
        {
            try
            {
                if (_sck.Connected == true)
                {
                    _sck.Client.Shutdown(SocketShutdown.Both);
                }

                _sck        = null;
                _sck        = new StarBucksSocket();
                _sck.remote = url;
                _sck.port   = port;

                // Timeout 조절
                _sck.SendTimeout    = 3;
                _sck.ReceiveTimeout = 3;
                _sck.recentTime     = DateTime.Now;
                _sck.finalTime      = DateTime.Now;

                // Socket 연결
                var result  = _sck.BeginConnect(url, port, null, null);
                var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(2), true);

                if (!success)
                {
                    connectEvent?.Invoke(this, new connectedArgs(url, port, false));
                    throw new Exception("Timeout");
                }

                _sck.recentTime = DateTime.Now;
                connectEvent?.Invoke(this, new connectedArgs(url, port, true));
                ListenforMessage();
            }
            catch (Exception)
            {
                throw new Exceptions.socketConnect("서버에 연결할 수 없습니다. 서버 주소, 인터넷 연결을 확인하십시오.");
            }
        }