/// <summary>
        /// Execute.
        /// </summary>
        /// <param name="args">The arguments.</param>
        /// <param name="client">The client.</param>
        /// <returns></returns>
        public bool Execute(string[] args, TcpClient client = null)
        {
            NetworkStream stream = client.GetStream();
            StreamReader  reader = new StreamReader(stream);

            SendAndRecieve.RecieveInfo(reader);
            client.GetStream().Flush();
            client.GetStream().Close();
            client.Close();

            return(false);
        }
示例#2
0
        /// <summary>
        /// Connect.
        /// </summary>
        public void Connect()
        {
            string ipAdresss = ConfigurationManager.AppSettings["IP"].ToString();
            string strPort   = ConfigurationManager.AppSettings["port"].ToString();
            int    port      = Int32.Parse(strPort);

            while (true)
            {
                // Send data to server
                string message = Console.ReadLine();

                Task task = new Task(() =>
                {
                    bool connectionAlive = true;
                    TcpClient client     = new TcpClient();
                    IPEndPoint ep        = new IPEndPoint(IPAddress.Parse(ipAdresss), port);

                    // comnect:
                    client.Connect(ep);

                    using (NetworkStream stream = client.GetStream())
                        using (StreamReader reader = new StreamReader(stream))
                            using (StreamWriter writer = new StreamWriter(stream))
                            {
                                while (connectionAlive)
                                {
                                    string[] arr      = message.Split(' ');
                                    string commandKey = arr[0];

                                    SendAndRecieve.Send(writer, message);

                                    // check whats next (close connection or continue)
                                    if (commands.ContainsKey(commandKey))
                                    {
                                        string[] arguments = arr.Skip(1).ToArray();
                                        ICommand command   = commands[commandKey];
                                        connectionAlive    = command.Execute(arguments, client);

                                        if (connectionAlive)
                                        {
                                            // NEED other input
                                            message = Console.ReadLine();
                                        }
                                    }
                                }
                            }
                    client.Close();
                });
                task.Start();
                task.Wait();
            }
        }
示例#3
0
        /// <summary>
        /// Starts the game.
        /// </summary>
        public static void StartGame()
        {
            gameAlive = true;
            new Task(() => {
                // wait for join
                NetworkStream stream = server.GetStream();
                StreamReader reader  = new StreamReader(stream);

                while (gameAlive)
                {
                    bool answer = SendAndRecieve.RecieveInfo(reader);
                }
            }).Start();
        }