/// <summary> /// 关闭指定的客户端 /// </summary> /// <param name="client"></param> public void Close(Socket client) { TcpClientInfo TheClient = null; foreach (TcpClientInfo clientInfo in Clients) { if (client.Equals(clientInfo.socket)) { TheClient = clientInfo; } } if (TheClient == null) { return; } if (TheClient.client.Connected) { TheClient.socket.Shutdown(SocketShutdown.Both); } //关闭Socket TheClient.socket.Close(); //关闭tcpClient TheClient.client.Close(); Clients.Remove(TheClient); TheClient = null; }
private void DoAcceptTcpClient(IAsyncResult ar) { if (stopListeing) { return; } //还原原始的TcpListner对象 Server = (TcpListener)ar.AsyncState; if (Server == null) { return; } //完成连接的动作,并返回新的TcpClient TcpClient client = Server.EndAcceptTcpClient(ar); TcpClientInfo internalClient = new TcpClientInfo(client, RecvDataBufferSize); //Console.WriteLine(internalClient.Ip + ":" + internalClient.Port + "连接成功"); //更新链接入的客户端数量,将客户端保存至已连接客户端列表中 lock (Clients) { Clients.Add(internalClient); } OnConnnectServer?.Invoke(client.Client); internalClient.socket.BeginReceive(internalClient.RecvBuff, 0, internalClient.RecvBuff.Length, SocketFlags.None, new AsyncCallback(DoReceiveData), internalClient); Server.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpClient), Server); }
private void DoReceiveData(IAsyncResult ar) { if (stopRecieve) { return; } TcpClientInfo internalClient = (TcpClientInfo)ar.AsyncState; NetworkStream networkStream; int read = 0; try { networkStream = internalClient.client.GetStream(); read = networkStream.EndRead(ar); } catch { read = 0; } if (read == 0) { ///客户端已经断开 lock (Clients) { Clients.Remove(internalClient); } //触发客户端断开事件 OnDisconnectServer?.Invoke(internalClient.socket); try { internalClient.socket.Close(); internalClient.socket.Dispose(); internalClient = null; } catch (Exception e) { } return; } byte[] recvData = new byte[read]; Array.Copy(internalClient.RecvBuff, 0, recvData, 0, read); //string ssss = Encoding.UTF8.GetString(recvData); //将收到的消息存入队列 TCPMessage message = new TCPMessage(internalClient.client.Client, this.socket, recvData); recvQueue.Enqueue(message); //发送消息到达通知 OnMessageArrived?.Invoke(); //OnRecivedMessage?.Invoke(internalClient.socket, recvData); //Console.WriteLine(internalClient.Ip + ":" + internalClient.Port + "有消息:" + ssss); internalClient.socket.BeginReceive(internalClient.RecvBuff, 0, internalClient.RecvBuff.Length, SocketFlags.None, new AsyncCallback(DoReceiveData), internalClient); }
private TcpClientInfo FindTcpClinet(Socket socket) { TcpClientInfo info = null; foreach (TcpClientInfo client in Clients) { if (client.socket == socket) { info = client; break; } } return(info); }
/// <summary> /// 这个服务器类设计为向外暴露Socket,自定义的中间类不要暴露 /// </summary> /// <param name="Ip"></param> /// <param name="Port"></param> /// <returns></returns> private TcpClientInfo FindTcpClinet(string Ip, int Port) { TcpClientInfo info = null; foreach (TcpClientInfo client in Clients) { if (client.Ip == Ip && client.Port == Port) { info = client; break; } } return(info); }
private void DoSendMsg() { Console.WriteLine("发送线程启动"); while (!stopSend) { if (sendQueue.Count > 0) { TCPMessage message = sendQueue.Dequeue(); //Console.WriteLine("要发送消息啦" + message.Message); //没有指定发送目标 向所有连接的客户端发送 if (message.TargetPoint == null) { foreach (TcpClientInfo tcpinfo in Clients) { tcpinfo.socket.Send(message.data, message.data.Length, SocketFlags.None); } OnMessageSend?.Invoke(null); } else { TcpClientInfo clientInfo = FindTcpClinet(message.TargetPoint); Socket To = null; //通过Socket没有找到 if (clientInfo == null) { To = FindClient(message.TargetIP, message.TargetPort); if (To == null) { //TODO:报错,没有找到客户端 Error?.Invoke("发送数据时没有找到目标"); continue; } } else { To = clientInfo.socket; } To.Send(message.data, message.data.Length, SocketFlags.None); OnMessageSend?.Invoke(To); } } Thread.Sleep(10); } }