Example #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Amion.Network SimpleChat Server\n");

            Console.WriteLine("Type '!start' to start the server");
            Console.WriteLine("Type '!help' for all commands\n");

            //Create the network server
            server = new NetServer();

            //Subscribe to Connection Added/Removed event
            server.ConnectionAdded   += Server_ConnectionAdded;
            server.ConnectionRemoved += Server_ConnectionRemoved;

            //Create a message handler for incoming network messages
            messageHandler = new NetMessageHandler();

            //Subscribe to the MessageReceived event where we will handle all incoming network messages
            messageHandler.MessageReceived += MessageHandler_MessageReceived;

            //Start the message processor thread
            messageHandler.StartMessageProcessor();

            //Begin Loop to keep the program running
            bool exit = false;

            do
            {
                string command = Console.ReadLine();

                switch (command)
                {
                case "!start":
                {
                    Console.WriteLine("Starting the server");

                    //Start the server's listener socket on IP: 127.0.0.1:6695
                    server.StartListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6695));

                    break;
                }

                case "!stop":
                {
                    Console.WriteLine("Stopping the server");

                    server.StopListener();
                    server.DisconnectAll();

                    break;
                }

                case "!exit":
                {
                    Console.WriteLine("Exiting the program");
                    exit = true;
                    break;
                }

                case "!help":
                {
                    Console.WriteLine("Type '!exit' to quit the program");
                    Console.WriteLine("Type '!stop' to stop the server");
                    Console.WriteLine("Type '!start' to start the server");
                    break;
                }

                default:
                {
                    Console.WriteLine("Unknown command");
                    break;
                }
                }
            } while (!exit);
        }
Example #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Amion.Network SimpleChat Client\n");

            Console.WriteLine("Type '!connect' to connect to the server");
            Console.WriteLine("Type '!help' for all commands\n");

            //Create the network client
            client = new NetClient();

            //Subscribe to Connection Added/Removed event
            client.ConnectionAdded   += Client_ConnectionAdded;
            client.ConnectionRemoved += Client_ConnectionRemoved;

            //Create a message handler for incoming network messages
            messageHandler = new NetMessageHandler();

            //Subscribe to the MessageReceived event where we will handle all incoming network messages
            messageHandler.MessageReceived += MessageHandler_MessageReceived;

            //Start the message processor thread
            messageHandler.StartMessageProcessor();

            //Begin Loop to keep the program running
            bool exit = false;

            do
            {
                string command = Console.ReadLine();

                switch (command)
                {
                case "!connect":
                {
                    Console.WriteLine("Connecting to the server");

                    //Connect to IP: 127.0.0.1:6695
                    client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6695));

                    break;
                }

                case "!disconnect":
                {
                    Console.WriteLine("Disconnecting from the server");
                    client.Disconnect();
                    break;
                }

                case "!exit":
                {
                    Console.WriteLine("Exiting the program");
                    exit = true;
                    break;
                }

                case "!help":
                {
                    Console.WriteLine("Type '!exit' to quit the program");
                    Console.WriteLine("Type '!disconnect' to disconnect from the server");
                    Console.WriteLine("Type '!connect' to connect to the server");
                    break;
                }

                default:
                {
                    //Create an outgoing network message for the chat message
                    NetOutMessage netMessage = new NetOutMessage();
                    netMessage.Write(command);
                    netMessage.Finish();

                    //Send the message if possible
                    client.Connection?.Send(netMessage);

                    break;
                }
                }
            } while (!exit);
        }