void Broadcast2Clients(string data) { foreach (TcpClient s in _tcpClients) { NetworkStreamP tmp = new NetworkStreamP(s.GetStream()); tmp.Write(data); } }
public void OnRecvData(object sender, EventArgs e) { TcpClient tcpClient = sender as TcpClient; int threadID = Port.TCP_USER_FILE_PORT % 10000; using (NetworkStreamP buf = new NetworkStreamP(tcpClient.GetStream())) { buf.ReceiveBufferSize = tcpClient.ReceiveBufferSize; while (true) { try { IPEndPoint where = tcpClient.Client.RemoteEndPoint as IPEndPoint; IPAddress clientIP = where.Address; string question; buf.Read(out question); string[] result = question.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); Registry.AddKey2Registry("PublishClient", "CurrentUserID", result[0]); Registry.AddKey2Registry("PublishClient", "CurrentUserAccount", result[1]); Registry.AddKey2Registry("PublishClient", "CurrentUserName", result[2]); isCatch = true; } catch (Exception ex) { Type type = ex.GetType(); if (type == typeof(TimeoutException)) { // 超时异常,不中断连接 Console.WriteLine("{0} [client {1}]: 数据超时失败!", DateTime.Now, threadID); } else { // 仍旧抛出异常,中断连接 Console.WriteLine("{0} [client {1}]: 中断连接异常原因:{2}!", DateTime.Now, threadID, type.Name); throw ex; } } } } }
public void OnListenBookInfo(object sender, EventArgs e) { TcpClient tcpClient = sender as TcpClient; int threadID = Port.TCP_BOOK_INFORMATION_PORT % 10000; Console.WriteLine("On Listen..."); using (NetworkStreamP buf = new NetworkStreamP(tcpClient.GetStream())) { buf.ReceiveBufferSize = tcpClient.ReceiveBufferSize; while (true) { try { byte[] raw; buf.Read(out raw); MemoryStream ms = new MemoryStream(raw); BinaryFormatter bf = new BinaryFormatter(); BookInformationList another = bf.Deserialize(ms) as BookInformationList; Invoke(new AddBookInfoList_dele(AddBookInfoList), new object[] { another }); } catch (Exception ex) { Type type = ex.GetType(); if (type == typeof(TimeoutException)) { // 超时异常,不中断连接 Console.WriteLine("{0} [host {1}]: 数据超时失败!", DateTime.Now, threadID); } else { // 仍旧抛出异常,中断连接 Console.WriteLine("{0} [host {1}]: 中断连接异常原因:{2}!", DateTime.Now, threadID, type.Name); throw ex; } } } } }
public void OnListenClient(object sender, EventArgs e) { TcpClient tcpClient = sender as TcpClient; int threadID = Port.TCP_LOGIN_PORT % 10000; using (NetworkStreamP buf = new NetworkStreamP(tcpClient.GetStream())) { buf.ReceiveBufferSize = tcpClient.ReceiveBufferSize; while (true) { try { IPEndPoint where = tcpClient.Client.RemoteEndPoint as IPEndPoint; IPAddress clientIP = where.Address; string question, answer; buf.Read(out question); string[] result = question.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); switch (result[0]) { case "Login": OnClientLogin(ref clientIP, ref result, out answer); break; case "Reg": OnClientReg(ref result, out answer); break; case "Logoff": OnClientLogoff(ref result, out answer); break; case "Change": OnClientChange(ref result, out answer); break; default: answer = VerMessage.DEFAULT_RESPONSE; break; } buf.Write(answer); } catch (Exception ex) { Type type = ex.GetType(); if (type == typeof(TimeoutException)) { // 超时异常,不中断连接 Console.WriteLine("{0} [host {1}]: 数据超时失败!", DateTime.Now, threadID); } else { // 仍旧抛出异常,中断连接 Console.WriteLine("{0} [host {1}]: 中断连接异常原因:{2}!", DateTime.Now, threadID, type.Name); throw ex; } } } } }
public void OnListen(object sender, EventArgs e) { // Template TcpClient tcpClient = sender as TcpClient; int threadID = 7777; Console.WriteLine("On Listen..."); using (NetworkStreamP buf = new NetworkStreamP(tcpClient.GetStream())) { buf.ReceiveBufferSize = tcpClient.ReceiveBufferSize; while (true) { try { string q, a; buf.Read(out q); a = q.ToUpper(); buf.Write(a); DateTime now = DateTime.Now; IPEndPoint where = tcpClient.Client.RemoteEndPoint as IPEndPoint; Console.WriteLine("{0} [host {1}]: receive message from [{2}:{3}]", now, threadID, where.Address.ToString(), where.Port.ToString()); Console.WriteLine("{0} [host {1}]: receive message [{2}]", now, threadID, q); } catch (Exception ex) { Type type = ex.GetType(); if (type == typeof(TimeoutException)) { // 超时异常,不中断连接 Console.WriteLine("{0} [host {1}]: 数据超时失败!", DateTime.Now, threadID); } else { // 仍旧抛出异常,中断连接 Console.WriteLine("{0} [host {1}]: 中断连接异常原因:{2}!", DateTime.Now, threadID, type.Name); throw ex; } } } } }