/// <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; } }
public async void ReadClientData(IAsyncResult ar) { try { if (!TcpClient.Connected) { Disconnected(); return; } else { int Recevied = NetStream.EndRead(ar); if (Recevied > 0) { BytesRecevied += Recevied; await ClientMS.WriteAsync(PacketBuffer, 0, Recevied); if (!Identified && BytesRecevied >= 4) { int iRet = GetInteger(); Identified = true; //One node notify to main UI if (OnConnected != null) { OnConnected.Invoke(this); } } else if (Identified) { Process(); } NetStream.BeginRead(PacketBuffer, 0, PacketBuffer.Length, ReadClientData, null); } else { Disconnected(); return; } } } catch { Disconnected(); return; } }
/// <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); }