public void SendData(string SendData) { try { if (client == null || !client.Connected) { Sockets sks = new Sockets(); sks.ex = new Exception("客户端无连接.."); sks.ClientDispose = true; pushSockets.Invoke(sks); //推送至netstat } if (client.Connected) //如果连接则发送 { if (nStream == null) { nStream = client.GetStream(); } byte[] buffer = Encoding.UTF8.GetBytes(SendData); nStream.Write(buffer, 0, buffer.Length); } } catch (Exception skex) { Sockets sks = new Sockets(); sks.ex = skex; sks.ClientDispose = true; pushSockets.Invoke(sks);//推送至netstat } }
/// <summary> /// 读取 /// </summary> private void EndReader(IAsyncResult ir) { Sockets s = ir.AsyncState as Sockets; try { if (s != null) { if (IsClose && client == null) { sk.nStream.Close(); sk.nStream.Dispose(); return; } s.Offset = s.nStream.EndRead(ir); if (pushSockets != null) { pushSockets.Invoke(s);//推送至UI } sk.nStream.BeginRead(sk.RecBuffer, 0, sk.RecBuffer.Length, new AsyncCallback(EndReader), sk); } } catch (Exception skex) { Sockets sks = s; sks.ex = skex; sks.ClientDispose = true; if (pushSockets != null) { pushSockets.Invoke(sks);//推送至UI } } }
private void EndReader(IAsyncResult ir) { Sockets s = ir.AsyncState as Sockets; try { if (s != null) { if (_isClose && TcpClient == null) { _sk.NetworkStream.Close(); _sk.NetworkStream.Dispose(); return; } s.Offset = s.NetworkStream.EndRead(ir); PushSockets.Invoke(s);//推送至UI _sk.NetworkStream.BeginRead(_sk.RecBuffer, 0, _sk.RecBuffer.Length, new AsyncCallback(EndReader), _sk); } } catch (Exception skex) { Sockets sks = s; if (sks != null) { sks.Ex = skex; sks.ClientDispose = true; PushSockets.Invoke(sks); //推送至UI } } }
private void Connect() { try { TcpClient.Connect(_ip); _networkStream = new NetworkStream(TcpClient.Client, true); _sk = new Sockets(_ip, TcpClient, _networkStream); _sk.NetworkStream.BeginRead(_sk.RecBuffer, 0, _sk.RecBuffer.Length, new AsyncCallback(EndReader), _sk); //推送连接成功. Sockets sks = new Sockets { ErrorCode = Sockets.ErrorCodes.ConnectSuccess, Ex = new Exception("客户端连接成功."), ClientDispose = false }; PushSockets.Invoke(sks); } catch (Exception skex) { Sockets sks = new Sockets { ErrorCode = Sockets.ErrorCodes.ConnectError, Ex = new Exception("客户端连接失败..异常信息:" + skex.Message), ClientDispose = true }; PushSockets.Invoke(sks); } }
/// <summary> /// 异步接收发送的信息. /// </summary> /// <param name="ir"></param> private void EndReader(IAsyncResult ir) { Sockets sks = ir.AsyncState as Sockets; if (sks != null && _listener != null) { try { if (sks.NewClientFlag || sks.Offset != 0) { sks.NewClientFlag = false; sks.Offset = sks.NetworkStream.EndRead(ir); PushSockets.Invoke(sks);//推送至UI sks.NetworkStream.BeginRead(sks.RecBuffer, 0, sks.RecBuffer.Length, new AsyncCallback(EndReader), sks); } } catch (Exception skex) { lock (_obj) { //移除异常类 ClientList.Remove(sks); Sockets sk = sks; sk.ClientDispose = true; //客户端退出 sk.Ex = skex; PushSockets.Invoke(sks); //推送至UI } } } }
/// <summary> /// 启动监听,并处理连接 /// </summary> public override void Start() { try { _listener.Start(); Thread accTh = new Thread(new ThreadStart(delegate { while (true) { if (_isStop != false) { break; } GetAcceptTcpClient(); Thread.Sleep(1); } })); accTh.Start(); } catch (SocketException skex) { Sockets sks = new Sockets(); sks.Ex = skex; PushSockets.Invoke(sks);//推送至UI } }
/// <summary> /// 服务端启动监听,处理链接 /// </summary> public override void Start() { try { Listener.Start(); Thread Accth = new Thread(new ThreadStart( delegate { while (true) { if (IsStop != false) { break; } this.GetAcceptTcpClient(); Thread.Sleep(1); } } )); Accth.Start(); } catch (SocketException skex) { Sockets sks = new Sockets(); sks.ex = skex; pushSockets.Invoke(sks); } }
/// <summary> /// 向某一位客户端发送信息 /// </summary> /// <param name="ip">客户端IP+端口地址</param> /// <param name="sendData">发送的数据包</param> public void SendToClient(IPEndPoint ip, string sendData) { try { Sockets sks = ClientList.Find(o => Equals(o.Ip, ip)); if (sks != null) { if (sks.Client.Connected) { //获取当前流进行写入. NetworkStream nStream = sks.NetworkStream; if (nStream.CanWrite) { byte[] buffer = Encoding.UTF8.GetBytes(sendData); nStream.Write(buffer, 0, buffer.Length); } else { //避免流被关闭,重新从对象中获取流 nStream = sks.Client.GetStream(); if (nStream.CanWrite) { byte[] buffer = Encoding.UTF8.GetBytes(sendData); nStream.Write(buffer, 0, buffer.Length); } else { //如果还是无法写入,那么认为客户端中断连接. ClientList.Remove(sks); } } } else { //没有连接时,标识退出 Sockets ks = new Sockets(); sks.ClientDispose = true; //如果出现异常,标识客户端下线 sks.Ex = new Exception("客户端无连接"); PushSockets.Invoke(sks); //推送至UI } } } catch (Exception skex) { Sockets sks = new Sockets { ClientDispose = true, Ex = skex }; //如果出现异常,标识客户端退出 if (PushSockets != null) { PushSockets.Invoke(sks);//推送至UI } } }
/// <summary> /// 发送消息 /// </summary> /// <param name="sendData"></param> public void SendData(string sendData) { try { //如果连接则发送 if (TcpClient != null) { if (TcpClient.Connected) { if (_networkStream == null) { _networkStream = TcpClient.GetStream(); } byte[] buffer = Encoding.UTF8.GetBytes(sendData); _networkStream.Write(buffer, 0, buffer.Length); } else { Sockets sks = new Sockets { ErrorCode = Sockets.ErrorCodes.TrySendData, Ex = new Exception("客户端发送时无连接,开始进行重连上端.."), ClientDispose = true }; PushSockets.Invoke(sks);//推送至UI RestartInit(); } } else { Sockets sks = new Sockets { ErrorCode = Sockets.ErrorCodes.TrySendData, Ex = new Exception("客户端对象为null,开始重连上端.."), ClientDispose = true }; PushSockets.Invoke(sks);//推送至UI RestartInit(); } } catch (Exception skex) { Sockets sks = new Sockets { ErrorCode = Sockets.ErrorCodes.TrySendData, Ex = new Exception("客户端出现异常,开始重连上端..异常信息:" + skex.Message), ClientDispose = true }; PushSockets.Invoke(sks);//推送至UI RestartInit(); } }
public override void Stop() { Sockets sks = new Sockets(); if (TcpClient != null) { TcpClient.Client.Shutdown(SocketShutdown.Both); Thread.Sleep(10); TcpClient.Close(); _isClose = true; TcpClient = null; } else { sks.Ex = new Exception("客户端没有初始化.!"); } sks.Ex = new Exception("客户端与上端断开连接.."); PushSockets.Invoke(sks);//推送至UI }
/// <summary> /// 等待处理新的连接 /// </summary> private void GetAcceptTcpClient() { try { _semap.WaitOne(); TcpClient tclient = _listener.AcceptTcpClient(); //维护客户端队列 Socket socket = tclient.Client; NetworkStream stream = new NetworkStream(socket, true); //承载这个Socket Sockets sks = new Sockets(tclient.Client.RemoteEndPoint as IPEndPoint, tclient, stream); sks.NewClientFlag = true; //加入客户端集合. AddClientList(sks); //推送新客户端 PushSockets.Invoke(sks); //客户端异步接收 sks.NetworkStream.BeginRead(sks.RecBuffer, 0, sks.RecBuffer.Length, new AsyncCallback(EndReader), sks); //主动向客户端发送一条连接成功信息 if (stream.CanWrite) { byte[] buffer = Encoding.UTF8.GetBytes(boundary); stream.Write(buffer, 0, buffer.Length); } _semap.Release(); } catch (Exception exs) { _semap.Release(); Sockets sk = new Sockets { ClientDispose = true, Ex = new Exception(exs.ToString() + "新连接监听出现异常") }; //客户端退出 if (PushSockets != null) { PushSockets.Invoke(sk);//推送至UI } } }
/// <summary> /// 连接 /// </summary> private bool Connect() { try { client.Connect(ip); nStream = new NetworkStream(client.Client, true); sk = new Sockets(ip, client, nStream); sk.nStream.BeginRead(sk.RecBuffer, 0, sk.RecBuffer.Length, new AsyncCallback(EndReader), sk); return(true); } catch (Exception skex) { Sockets sks = new Sockets(); sks.ex = skex; sks.ClientDispose = true; if (pushSockets != null) { pushSockets.Invoke(sks);//推送至UI } return(false); } }