Exemple #1
0
        static void Main(string[] args)
        {
            int port = 9999;

            Console.WriteLine("Tcp chat host");

            Console.WriteLine($"Waiting on a client on port: {port}");

            using (TcpTransceiver transceiver = TcpTransceiver.Listen(port))
            {
                Console.WriteLine("Client connected");

                TcpBlock block = new TcpBlock(transceiver);

                block.ReadBlock.LinkTo(new ActionBlock <byte[]>(v =>
                {
                    Console.Write($"{Encoding.ASCII.GetString(v)}");
                }));

                while (transceiver.IsConnected)
                {
                    block.WriteBlock.Post(Encoding.ASCII.GetBytes(Console.ReadLine()));
                }

                Console.WriteLine("Client disconnected");
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            int       port = 9999;
            IPAddress host = IPAddress.Parse(args.Length > 0 ? args[0] : "127.0.0.1");

            Console.WriteLine("Tcp chat client");

            Console.WriteLine($"Connecting to host: {host.ToString()}:{port}");

            using (TcpTransceiver transceiver = TcpTransceiver.Connect(host, port))
            {
                Console.WriteLine("Client connected");

                TcpBlock block = new TcpBlock(transceiver);

                block.ReadBlock.LinkTo(new ActionBlock <byte[]>(v =>
                {
                    Console.Write($"{Encoding.ASCII.GetString(v)}");
                }));

                while (transceiver.IsConnected)
                {
                    block.WriteBlock.Post(Encoding.ASCII.GetBytes(Console.ReadLine()));
                }

                Console.WriteLine("Client disconnected");
            }
        }
Exemple #3
0
        public TcpBlock(TcpTransceiver transceiver)
        {
            Transceiver = transceiver;

            WriteBlock = new ActionBlock <byte[]>(v =>
            {
                Transceiver.WriteData(v);
            });

            Task.Run(() =>
            {
                while (Transceiver.IsConnected)
                {
                    byte[] data = Transceiver.ReadData();
                    if (data.Length > 0)
                    {
                        ReadBlock.Post(data);
                    }
                }
            });
        }