示例#1
0
        public static void Listen()
        {
            if (Listener != null && Accept)
            {
                while (true)
                {
                    System.Console.WriteLine("Waiting for client");
                    var clientTask = Listener.AcceptTcpClientAsync();

                    if (clientTask.Result != null)
                    {
                        System.Console.WriteLine("Client connected");

                        var client  = clientTask.Result;
                        var message = string.Empty;

                        var cart = new Cart();

                        while (message != null && !message.StartsWith("EXIT"))
                        {
                            System.Console.WriteLine($"Received {message}.");

                            var clientData = new byte[ClientMesageSize];
                            client.GetStream().Read(clientData, 0, clientData.Length);

                            message = Encoding.ASCII.GetString(clientData);

                            var confirmationMessage = "MessageReceived...Wait for a response";
                            client.GetStream().Write(Encoding.ASCII.GetBytes(confirmationMessage), 0, confirmationMessage.Length);

                            var cartOperations = new CartOperations(cart);

                            var operationResult = cartOperations.ProcessCommand(message);

                            System.Console.WriteLine($"Sending back: {operationResult}.");

                            client.GetStream().Write(Encoding.ASCII.GetBytes(operationResult), 0, operationResult.Length);
                        }

                        System.Console.WriteLine("EXIT received...closing client!");
                        client.GetStream().Dispose();
                        client.Dispose();
                    }
                }
            }
        }
示例#2
0
        async void HandleRequests()
        {
            try
            {
                while (true)
                {
                    var client = await Listener.AcceptTcpClientAsync();

                    HandleRequest(client);
                }
            }
            catch (SocketException)
            {
            }
            catch (Exception e)
            {
                Console.WriteLine($"Got error: {e.Message}");
            }

            Console.WriteLine("Stopping");
        }
示例#3
0
        public static void Listen()
        {
            if (Listener != null && Accept)
            {
                // Continue listening.
                while (true)
                {
                    Console.WriteLine("Waiting for client...");
                    var clientTask = Listener.AcceptTcpClientAsync(); // Get the client

                    if (clientTask.Result != null)
                    {
                        Console.WriteLine("Client connected. Waiting for data.");
                        var client = clientTask.Result;

                        byte[] buffer = new byte[1024];
                        client.GetStream().Read(buffer, 0, buffer.Length);
                        var data = CleanMessage(buffer);
                        Console.Write(data);

                        using (StreamWriter writer = new StreamWriter("XmlFile.xml", false))
                        {
                            writer.Write(data, 0, data.Length);
                        }



                        byte[] message = Encoding.ASCII.GetBytes("Saving your data...");
                        client.GetStream().Write(message, 0, message.Length);

                        Console.WriteLine("Closing connection.");
                        client.GetStream().Dispose();
                    }
                }
            }
        }