public override bool Analysis() { List <string> analysisinfor = base.Split(3); List <Customer> customers = CustomerCollector.FindCustomers(analysisinfor[1]); if (customers != null && customers.Count > 0) { RecvChatContent cmd = new RecvChatContent(); cmd._Content = new ChatContent() { _FromUID = int.Parse(analysisinfor[0]), _ToUId = int.Parse(analysisinfor[1]), _Text = analysisinfor[2], _FromSendoutTime = DateTime.Now }; byte[] bytecmd = cmd.GetProtocolCommand(); foreach (Customer client in customers) { client._SrcSocket.Send(bytecmd); } } return(true); }
/// <summary> /// 开始侦听,侦听成功才开启接收 /// </summary> /// <param name="socketResult">socket操作结果</param> /// <returns>侦听是否成功</returns> public bool StartListen(int serverPort) { #region try { CustomerCollector.Init(); IPEndPoint localEP = new IPEndPoint(IPAddress.Any, serverPort); _tcpServer = new Socket(localEP.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); _tcpServer.Bind(localEP); _tcpServer.Listen(100); _thdReceive = new Thread(new ThreadStart(receiveSocket)); _thdReceive.IsBackground = true; _thdReceive.Start(); (new TcpOnlineListener()).Start(); return(true); } catch (Exception e) { this.writeError(e); return(false); } #endregion }
/// <summary> /// 从客户端接收信息 /// </summary> /// <param name="iar"></param> private void receiveData(IAsyncResult iar) { #region Socket client = null; try { client = (Socket)iar.AsyncState; int recvcount = client.EndReceive(iar); if (recvcount > 0) { if (this.dispatcher(client, recvcount)) { client.BeginReceive(_recvDataBuffer, 0, _recvDataBuffer.Length, SocketFlags.None, new AsyncCallback(receiveData), client); } } } catch (SocketException e) { CustomerCollector.Remove(client); this.writeError(e); } catch (Exception e) { this.writeError(e); } #endregion }
public override bool Analysis() { lock (lockcustomer) { List <string> analysisinfor = base.Split(1); IPEndPoint endremotepoint = (System.Net.IPEndPoint)base._SourceClient.RemoteEndPoint; Customer customer = new Customer() { _UId = int.Parse(analysisinfor[0]), IPAddress = endremotepoint.Address.ToString(), Port = endremotepoint.Port, _SrcSocket = base._SourceClient }; //必须拿同通道的socket来发送在线命令 CustomerCollector.UpdateUserTime(customer); } return(true); }
public override bool Analysis() { List <string> analysisinfor = base.Split(1); IPEndPoint endremotepoint = (System.Net.IPEndPoint)base._SourceClient.RemoteEndPoint; Customer customer = new Customer() { _UId = int.Parse(analysisinfor[0]), IPAddress = endremotepoint.Address.ToString(), Port = endremotepoint.Port, _SrcSocket = base._SourceClient, _LogonTime = DateTime.Now }; Console.WriteLine("本次来源端口号:{0}", endremotepoint.Port); if (CustomerCollector.IsExist(customer) == null) { CustomerCollector.Add(customer); } return(true); }
private void btnViewOnline_Click(object sender, EventArgs e) { CustomerCollector.ViewToConsole(); }