/// <summary> /// Tries the connect. /// </summary> /// <exception cref='Exception'> /// Represents errors that occur during application execution. /// </exception> public void TryConnect() { try { IPEndPoint _ep = null; // if (m_bSecurityPolicy) { } else { _ep = this.GetServerAddress(); if (null == _ep) { ConnectState = EClientConnectState.CONNECT_STATE_FAILED; ClientLog.LogError("Connect timeout : no valid server ip or port"); return; } } ConnectState = EClientConnectState.CONNECT_STATE_TRY_CONNECT; _socketClient.BeginConnect(_ep, new AsyncCallback(ConnectCallback), _socketClient); ClientLog.Log("Connect server : " + _ep.Address.ToString() + ":" + _ep.Port.ToString()); } catch (Exception ex) { ClientConnectState = EClientConnectState.CONNECT_STATE_FAILED; ClientLog.LogError(ex.ToString()); } }
/** * */ private void ConnectCallback(IAsyncResult ar) { try { Socket _socket = (Socket)ar.AsyncState; if (!_socket.Connected) { ConnectState = EClientConnectState.CONNECT_STATE_FAILED; ClientLog.LogError(_socket.LocalEndPoint + " connect failed!, try connect again"); // this.DoRetryConnect (); } else { _socket.EndConnect(ar); this.StartRecevieMsg(); ConnectState = EClientConnectState.CONNECT_STATE_CONNECTED; ClientLog.Log(_socket.LocalEndPoint + " connect successful!"); } } catch (Exception e) { ConnectState = EClientConnectState.CONNECT_STATE_FAILED; // do something Console.WriteLine(e.ToString()); } }