public override void Send(string sessionKey, TcpSocketServer _server) { List <byte> list = new List <byte>(); list.Add((byte)this.Type); list.AddRange(Encoding.UTF8.GetBytes(this.Message)); _server.Broadcast(list.ToArray()); }
static void Main(string[] args) { NLogLogger.Use(); StartServer(); Console.WriteLine("TCP server has been started."); Console.WriteLine("Type something to send to client..."); while (true) { try { string text = Console.ReadLine(); if (text == "quit") { break; } else if (text == "many") { text = new string('x', 8192); for (int i = 0; i < 1000000; i++) { _server.Broadcast(Encoding.UTF8.GetBytes(text)); } } else if (text == "big") { text = new string('x', 1024 * 1024 * 100); _server.Broadcast(Encoding.UTF8.GetBytes(text)); } else { _server.Broadcast(Encoding.UTF8.GetBytes(text)); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } _server.Shutdown(); Console.WriteLine("TCP server has been stopped on [{0}].", _server.ListenedEndPoint); Console.ReadKey(); }
public void Broadcast(byte[] data) { if (!IsListening) { throw new InvalidOperationException("The server has stopped to listen."); } _server.Broadcast(data); }
/// <summary> /// 发送文件 /// </summary> /// <param name="path">文件路径</param> /// <param name="flag">文件类型</param> int SendFile(string sessionKey, TcpSocketServer _server) { if (!File.Exists(this.Message)) { OnMessageEvent("文件不存在"); Logs.Error("文件不存在"); return(FAIRED); } try { using (FileStream fsRead = new FileStream(this.Message, FileMode.OpenOrCreate, FileAccess.Read)) { byte[] buffer = new byte[1024 * 1024 * 3]; int r = fsRead.Read(buffer, 0, buffer.Length); List <byte> list = new List <byte>(); list.Add((byte)this.Type); list.AddRange(buffer); byte[] newBuffer = list.ToArray(); list.Clear(); if (sessionKey.Trim() == "") { _server.Broadcast(newBuffer, 0, r + 1); } else { _server.SendTo(sessionKey, newBuffer, 0, r + 1); } // MessageBox.Show("文件已发送~~"); OnMessageEvent("文件发送成功"); } return(SUCCESS); } catch (Exception ex) { Logs.Error("文件发送失败" + ex); OnMessageEvent("文件发送失败" + ex); throw new FileNotFoundException(); } }
public void Send(TcpSocketServer server) { server.Broadcast(GetBytes()); }