static void Main(string[] args) { msgData = Encoding.UTF8.GetBytes(RandomString(msgLength)); sendDelay = numClients * 20; using (server = new WatsonWsServer(hostname, port, false)) { #region Start-Server serverStats = new Statistics(); server.ClientConnected += (s, e) => { Console.WriteLine("Client connected: " + e.IpPort); lock (clientsLock) { clients.Add(e.IpPort); } }; server.ClientDisconnected += (s, e) => { Console.WriteLine("*** Client disconnected: " + e.IpPort); lock (clientsLock) { if (clients.Contains(e.IpPort)) { clients.Remove(e.IpPort); } } }; server.MessageReceived += (s, e) => { serverStats.AddRecv(e.Data.Length); }; // server.Logger = Logger; server.Start(); #endregion #region Start-and-Wait-for-Clients for (int i = 0; i < numClients; i++) { Console.WriteLine("Starting client " + (i + 1) + "..."); Task.Run(() => ClientTask()); Task.Delay(250).Wait(); } while (true) { Task.Delay(1000).Wait(); int connected = 0; lock (clientsLock) { connected = clients.Count; } if (connected == numClients) { break; } Console.WriteLine(connected + " of " + numClients + " connected, waiting"); } Console.WriteLine("All clients connected!"); serverReady = true; #endregion #region Send-Messages-to-Clients for (int i = 0; i < messagesPerClient; i++) { for (int j = 0; j < numClients; j++) { server.SendAsync(clients[j], msgData).Wait(); serverStats.AddSent(msgData.Length); } } #endregion #region Wait-for-Clients while (true) { Task.Delay(5000).Wait(); int remaining = 0; lock (clientsLock) { remaining = clients.Count; if (remaining < 1) { break; } Console.WriteLine(DateTime.Now.ToUniversalTime().ToString("HH:mm:ss.ffffff") + " waiting for " + remaining + " clients: "); foreach (string curr in clients) { Console.WriteLine("| " + curr); } } } #endregion #region Statistics Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine("Server statistics:"); Console.WriteLine(" " + serverStats.ToString()); Console.WriteLine(""); Console.WriteLine("Client statistics"); foreach (Statistics stats in clientStats) { Console.WriteLine(" " + stats.ToString()); } Console.WriteLine(""); #endregion } }
static void ClientTask() { Statistics stats = new Statistics(); using (WatsonWsClient client = new WatsonWsClient(hostname, port, false)) { #region Start-Client client.ServerConnected += (s, e) => { Console.WriteLine("Client detected connection to " + hostname + ":" + port); }; client.ServerDisconnected += (s, e) => { Console.WriteLine("Client disconnected from " + hostname + ":" + port); }; client.MessageReceived += (s, e) => { stats.AddRecv(e.Data.Length); }; // client.Logger = Logger; client.Start(); #endregion #region Wait-for-Server-Ready while (!serverReady) { Console.WriteLine("Client waiting for server..."); Task.Delay(2500).Wait(); } Console.WriteLine("Client detected server ready!"); #endregion #region Send-Messages-to-Server for (int i = 0; i < messagesPerClient; i++) { Task.Delay(sendDelay).Wait(); client.SendAsync(msgData).Wait(); stats.AddSent(msgData.Length); } #endregion #region Wait-for-Server-Messages while (stats.MsgRecv < messagesPerClient) { Task.Delay(1000).Wait(); } Console.WriteLine("Client exiting: " + stats.ToString()); lock (clientStatsLock) { clientStats.Add(stats); } #endregion } }