static void Main(string[] args) { // UDP server address string address = "127.0.0.1"; if (args.Length > 0) { address = args[0]; } // UDP server port int port = 3333; if (args.Length > 1) { port = int.Parse(args[1]); } Console.WriteLine($"UDP server address: {address}"); Console.WriteLine($"UDP server port: {port}"); Console.WriteLine(); // Create a new TCP chat client var client = new EchoClient(address, port); // Connect the client Console.Write("Client connecting..."); client.Connect(); Console.WriteLine("Done!"); Console.WriteLine("Press Enter to stop the client or '!' to reconnect the client..."); // Perform text input for (;;) { string line = Console.ReadLine(); if (line == string.Empty) { break; } // Disconnect the client if (line == "!") { Console.Write("Client disconnecting..."); client.Disconnect(); Console.WriteLine("Done!"); continue; } // Send the entered text to the chat server client.Send(line); } // Disconnect the client Console.Write("Client disconnecting..."); client.DisconnectAndStop(); Console.WriteLine("Done!"); }
static void Main(string[] args) { bool help = false; string address = "127.0.0.1"; int port = 3333; int clients = 100; int messages = 1000000; int size = 32; var options = new OptionSet() { { "h|?|help", v => help = v != null }, { "a|address=", v => address = v }, { "p|port=", v => port = int.Parse(v) }, { "c|clients=", v => clients = int.Parse(v) }, { "m|messages=", v => messages = int.Parse(v) }, { "s|size=", v => size = int.Parse(v) } }; try { options.Parse(args); } catch (OptionException e) { Console.Write("Command line error: "); Console.WriteLine(e.Message); Console.WriteLine("Try `--help' to get usage information."); return; } if (help) { Console.WriteLine("Usage:"); options.WriteOptionDescriptions(Console.Out); return; } Console.WriteLine($"Server address: {address}"); Console.WriteLine($"Server port: {port}"); Console.WriteLine($"Working clients: {clients}"); Console.WriteLine($"Messages to send: {messages}"); Console.WriteLine($"Message size: {size}"); // Prepare a message to send MessageToSend = new byte[size]; // Create echo clients var echoClients = new List <EchoClient>(); for (int i = 0; i < clients; ++i) { var client = new EchoClient(address, port, messages / clients); echoClients.Add(client); } TimestampStart = DateTime.UtcNow; // Connect clients Console.Write("Clients connecting..."); foreach (var client in echoClients) { client.Connect(); } Console.WriteLine("Done!"); foreach (var client in echoClients) { while (!client.Connected) { Thread.Yield(); } } Console.WriteLine("All clients connected!"); // Wait for processing all messages Console.Write("Processing..."); foreach (var client in echoClients) { while (client.IsConnected) { Thread.Sleep(100); } } Console.WriteLine("Done!"); Console.WriteLine(); Console.WriteLine($"Errors: {TotalErrors}"); Console.WriteLine(); Console.WriteLine($"Round-trip time: {Utilities.GenerateTimePeriod((TimestampStop - TimestampStart).TotalMilliseconds)}"); Console.WriteLine($"Total data: {Utilities.GenerateDataSize(TotalBytes)}"); Console.WriteLine($"Total messages: {TotalMessages}"); Console.WriteLine($"Data throughput: {Utilities.GenerateDataSize((long)(TotalBytes / (TimestampStop - TimestampStart).TotalSeconds))}/s"); if (TotalMessages > 0) { Console.WriteLine($"Message latency: {Utilities.GenerateTimePeriod((TimestampStop - TimestampStart).TotalMilliseconds / TotalMessages)}"); Console.WriteLine($"Message throughput: {(long)(TotalMessages / (TimestampStop - TimestampStart).TotalSeconds)} msg/s"); } }