Example #1
0
        static void Main(string[] args)
        {
            bool   help    = false;
            string address = "239.255.0.1";
            int    port    = 3333;
            int    clients = 100;
            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) },
                { "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($"Message size: {size}");

            // Prepare a message to send
            MessageToSend = new byte[size];

            // Create multicast clients
            var multicastClients = new List <MulticastClient>();

            for (int i = 0; i < clients; ++i)
            {
                var client = new MulticastClient("0.0.0.0", address, port);
                client.SetupMulticast(true);
                multicastClients.Add(client);
            }

            TimestampStart = DateTime.UtcNow;

            // Connect clients
            Console.Write("Clients connecting...");
            foreach (var client in multicastClients)
            {
                client.Connect();
            }
            Console.WriteLine("Done!");
            foreach (var client in multicastClients)
            {
                while (!client.Connected)
                {
                    Thread.Yield();
                }
            }
            Console.WriteLine("All clients connected!");

            // Sleep for 10 seconds...
            Console.Write("Processing...");
            Thread.Sleep(10000);
            Console.WriteLine("Done!");

            // Disconnect clients
            Console.Write("Clients disconnecting...");
            foreach (var client in multicastClients)
            {
                client.Disconnect();
            }
            Console.WriteLine("Done!");
            foreach (var client in multicastClients)
            {
                while (client.IsConnected)
                {
                    Thread.Yield();
                }
            }
            Console.WriteLine("All clients disconnected!");

            TimestampStop = DateTime.UtcNow;

            Console.WriteLine();

            Console.WriteLine($"Errors: {TotalErrors}");

            Console.WriteLine();

            TotalMessages = TotalBytes / size;

            Console.WriteLine($"Multicast 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");
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            // UDP listen address
            string listenAddress = "0.0.0.0";

            if (args.Length > 0)
            {
                listenAddress = args[0];
            }

            // UDP multicast address
            string multicastAddress = "239.255.0.1";

            if (args.Length > 1)
            {
                multicastAddress = args[1];
            }

            // UDP multicast port
            int multicastPort = 3334;

            if (args.Length > 2)
            {
                multicastPort = int.Parse(args[2]);
            }

            Console.WriteLine($"UDP listen address: {listenAddress}");
            Console.WriteLine($"UDP multicast address: {multicastAddress}");
            Console.WriteLine($"UDP multicast port: {multicastPort}");

            // Create a new TCP chat client
            var client = new MulticastClient(listenAddress, multicastPort);

            client.SetupMulticast(true);
            client.Multicast = multicastAddress;

            // 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;
                }
            }

            // Disconnect the client
            Console.Write("Client disconnecting...");
            client.DisconnectAndStop();
            Console.WriteLine("Done!");
        }