public bool Connect(bool bOnce = false) { m_bStartConnect = true; m_nReconnectTick = System.Environment.TickCount - m_nReconnectTime; if (bOnce) { if (m_TcpClient.Connect(m_sIP, m_nPort)) { m_bConnect = true; return(true); } return(false); } return(true); }
private void buttonStart_Click(object sender, EventArgs e) { try { string ip = textBoxIPAdress.Text.Trim(); ushort port = ushort.Parse(textBoxPort.Text.Trim()); appState = AppState.Starting; //Client组件发起连接的过程可以是同步或异步的 //同步是指组件的连接方法等到连接成功或失败了再返回(返回true或false) //异步是指组件的连接方法会立即返回,如果返回值为false则表示连接失败,如果连接成功则稍后会触发OnConnect事件 if (client.Connect(ip, port, checkBoxAsyncConn.Checked)) { if (checkBoxAsyncConn.Checked == false) { appState = AppState.Started; SetControlState(); } } else { appState = AppState.Stoped; SetControlState(); throw new Exception(string.Format("无法建立连接:{0},{1}", client.ErrorMessage, client.ErrorCode)); } } catch (Exception exc) { ShowMSG(exc.Message); } }
private void btnStart_Click(object sender, EventArgs e) { try { String ip = this.txtIpAddress.Text.Trim(); ushort port = ushort.Parse(this.txtPort.Text.Trim()); // 写在这个位置是上面可能会异常 SetAppState(AppState.Starting); AddMsg(string.Format("$Client Starting ... -> ({0}:{1})", ip, port)); if (client.Connect(ip, port, this.cbxAsyncConn.Checked)) { if (cbxAsyncConn.Checked == false) { SetAppState(AppState.Started); } AddMsg(string.Format("$Client Start OK -> ({0}:{1})", ip, port)); } else { SetAppState(AppState.Stoped); throw new Exception(string.Format("$Client Start Error -> {0}({1})", client.ErrorMessage, client.ErrorCode)); } } catch (Exception ex) { AddMsg(ex.Message); } }
/// <summary> /// 连接服务器 /// </summary> /// <param name="strIP"></param> /// <param name="uPort"></param> public void Connect(string strIP, ushort uPort) { SetAppState(AppState.Starting); client.Connect(strIP, uPort, true); //if (client.Connect(strIP, uPort, false)) //{ // SetAppState(AppState.Started); // AddLog("Conect OK"); // AddMsg("Conect OK"); //} //else //{ // SetAppState(AppState.Stoped); // AddLog("Connect Fail"); // AddMsg("Connect Fail"); //} }
/// <summary> /// 连接指定服务器Ip和Port /// </summary> /// <param name="ip"></param> /// <param name="port"></param> public void TCP_Start(string ip, ushort port) { try { if (client.Connect(ip, port)) { LogInfo?.Invoke("服务器 连接触发!!!"); } else { LogErr?.Invoke("服务器 连接失败!!!"); } } catch (Exception ex) { LogErr?.Invoke(ex.Message); } }