/// <summary> /// 等待处理新的连接 /// </summary> private void GetAcceptTcpClient() { try { semap.WaitOne(); TcpClient _tclient = TcpServer.AcceptTcpClient(); Socket _socket = _tclient.Client; NetworkStream _stream = new NetworkStream(_socket, true); //承载这个Socket TcpClientConnectSession _connectedSession = new TcpClientConnectSession(_tclient.Client.RemoteEndPoint as IPEndPoint, _tclient, _stream); _connectedSession.NewClientFlag = true; AddTcpClientConnecedSession(_connectedSession); _connectedSession.SkStream.BeginRead(recBuffer, 0, recBuffer.Length, new AsyncCallback(EndReader), _connectedSession); if (_stream.CanWrite) { } semap.Release(); } catch (Exception ex) { semap.Release(); RaiseDataReceivedEvent(TcpOperateEventCode.NewClientConnectError, null, ex, null, null); } }
/// <summary> /// 断开,移除所有终端链接 /// </summary> /// 时间:2016-04-12 19:19 /// 备注: public void ClearAllClients() { if (TcpClientConnectList != null) { for (int i = 0; i < TcpClientConnectList.Count; i++) { TcpClientConnectSession _connectedSession = TcpClientConnectList[i]; TcpClientConnectList.Remove(_connectedSession); _connectedSession.Close(); } } }
/// <summary> /// 连接到Server /// </summary> public void Connect() { try { ConnectTcpClient.Connect(TcpClientIpEndPoint); tcpClientStream = new NetworkStream(ConnectTcpClient.Client, true); TcpClientConnectedSeesion = new TcpClientConnectSession(TcpClientIpEndPoint, ConnectTcpClient, tcpClientStream); TcpClientConnectedSeesion.SkStream.BeginRead(recBuffer, 0, recBuffer.Length, new AsyncCallback(EndReader), TcpClientConnectedSeesion); RaiseDataReceivedEvent(TcpOperateEventCode.ConnectSuccess, null, null, TcpClientIpEndPoint, null); } catch (Exception ex) { RaiseDataReceivedEvent(TcpOperateEventCode.ConnectError, null, ex, TcpClientIpEndPoint, null); } }
/// <summary> /// 断开,移除某个终端连接 /// </summary> /// <param name="ip">IPEndPoint</param> /// 时间:2016-04-12 19:21 /// 备注: public void ClearClient(IPEndPoint ip) { if (TcpClientConnectList != null) { TcpClientConnectSession _connectedSession = TcpClientConnectList.Find(o => { return(o.Ip == ip); }); if (_connectedSession != null) { TcpClientConnectList.Remove(_connectedSession); } } }
/// <summary> /// 添加或更新终端连接 /// </summary> /// <param name="connectedSession">TcpClientConnectSession</param> private void AddTcpClientConnecedSession(TcpClientConnectSession connectedSession) { TcpClientConnectSession _connectedSession = TcpClientConnectList.Find(o => o.Ip == connectedSession.Ip); if (_connectedSession == null) { TcpClientConnectList.Add(connectedSession); } else { TcpClientConnectList.Remove(_connectedSession); TcpClientConnectList.Add(connectedSession); } RaiseDataReceivedEvent(TcpOperateEventCode.NewClientConnected, null, null, connectedSession.Ip, null); }
/// <summary> /// 向某一位客户端发送信息 /// </summary> /// <param name="ip">客户端IP+端口地址</param> /// <param name="sendDataBuffer">发送的数据包</param> public void SendToClient(IPEndPoint ip, byte[] sendDataBuffer) { try { TcpClientConnectSession _connectedSession = TcpClientConnectList.Find(o => { return(o.Ip == ip); }); if (_connectedSession != null) { if (_connectedSession.Client.Connected) { NetworkStream _stream = _connectedSession.SkStream; if (_stream.CanWrite) { byte[] _buffer = sendDataBuffer; _stream.Write(_buffer, 0, _buffer.Length); } else { _stream = _connectedSession.Client.GetStream(); if (_stream.CanWrite) { byte[] _buffer = sendDataBuffer; _stream.Write(_buffer, 0, _buffer.Length); } else { TcpClientConnectList.Remove(_connectedSession); RaiseDataReceivedEvent(TcpOperateEventCode.RemoveClientConnect, null, null, _connectedSession.Ip, null); } } } else { RaiseDataReceivedEvent(TcpOperateEventCode.NoClinets, null, null, _connectedSession.Ip, null); } } } catch (Exception ex) { RaiseDataReceivedEvent(TcpOperateEventCode.SendDataError, null, ex, ip, null); } }
/// <summary> /// 端口与Server的连接 /// </summary> public void Disconnect() { TcpClientConnectSession _connectedSession = new TcpClientConnectSession(); if (ConnectTcpClient != null) { ConnectTcpClient.Client.Shutdown(SocketShutdown.Both); Thread.Sleep(10); ConnectTcpClient.Close(); isClose = true; ConnectTcpClient = null; RaiseDataReceivedEvent(TcpOperateEventCode.CliendDisconnected, null, null, TcpClientIpEndPoint, null); } else { RaiseDataReceivedEvent(TcpOperateEventCode.ClientUninitialized, null, null, TcpClientIpEndPoint, null); } }
/// <summary> /// EndReader /// </summary> /// <param name="ir">IAsyncResult</param> private void EndReader(IAsyncResult ir) { TcpClientConnectSession _connectedSession = ir.AsyncState as TcpClientConnectSession; try { if (_connectedSession != null) { if (isClose && ConnectTcpClient == null) { TcpClientConnectedSeesion.SkStream.Close(); TcpClientConnectedSeesion.SkStream.Dispose(); return; } _connectedSession.Offset = _connectedSession.SkStream.EndRead(ir); byte[] _buffer = new byte[_connectedSession.Offset]; Array.Copy(recBuffer, _buffer, _connectedSession.Offset); if (_buffer != null) { string _readString = Encoding.UTF8.GetString(_buffer); if (string.Compare(_readString, "ServerOff", StringComparison.OrdinalIgnoreCase) == 0) { RaiseDataReceivedEvent(TcpOperateEventCode.ServerClose, null, null, _connectedSession.Ip, null); } else { RaiseDataReceivedEvent(TcpOperateEventCode.DataReceived, _buffer, null, _connectedSession.Ip, null); } } TcpClientConnectedSeesion.SkStream.BeginRead(recBuffer, 0, recBuffer.Length, new AsyncCallback(EndReader), TcpClientConnectedSeesion); } } catch (Exception ex) { RaiseDataReceivedEvent(TcpOperateEventCode.DataReceivedError, null, ex, TcpClientIpEndPoint, null); } }
/// <summary> /// 异步接收发送的信息. /// </summary> /// <param name="ir">IAsyncResult</param> private void EndReader(IAsyncResult ir) { TcpClientConnectSession _connectedSession = ir.AsyncState as TcpClientConnectSession; if (_connectedSession != null && TcpServer != null) { try { if (_connectedSession.NewClientFlag || _connectedSession.Offset != 0) { _connectedSession.NewClientFlag = false; _connectedSession.Offset = _connectedSession.SkStream.EndRead(ir); if (_connectedSession.Offset != 0) { byte[] _buffer = new byte[_connectedSession.Offset]; Array.Copy(recBuffer, _buffer, _connectedSession.Offset); RaiseDataReceivedEvent(TcpOperateEventCode.DataReceived, _buffer, null, _connectedSession.Ip, null); } else { TcpClientConnectList.Remove(_connectedSession);//移除连接终端 RaiseDataReceivedEvent(TcpOperateEventCode.ClientOffline, null, null, _connectedSession.Ip, null); } _connectedSession.SkStream.BeginRead(recBuffer, 0, recBuffer.Length, new AsyncCallback(EndReader), _connectedSession); } } catch (Exception ex) { lock (syncRoot) { TcpClientConnectList.Remove(_connectedSession); RaiseDataReceivedEvent(TcpOperateEventCode.DataReceivedError, null, ex, _connectedSession.Ip, null); } } } }