示例#1
0
        /// <summary>
        /// Demonstrates the client and server by using the classes directly with callback methods.
        /// </summary>
        /// <returns></returns>
        private async Task RunAsync2()
        {
            int port = 7777;

            var server = new AsyncTcpListener
            {
                IPAddress = IPAddress.IPv6Any,
                Port      = port,
                ClientConnectedCallback = tcpClient =>
                                          new AsyncTcpClient
                {
                    ServerTcpClient   = tcpClient,
                    ConnectedCallback = async(serverClient, isReconnected) =>
                    {
                        await Task.Delay(500);

                        byte[] bytes = Encoding.UTF8.GetBytes($"Hello, {tcpClient.Client.RemoteEndPoint}, my name is Server. Talk to me.");
                        await serverClient.Send(new ArraySegment <byte>(bytes, 0, bytes.Length));
                    },
                    ReceivedCallback = async(serverClient, count) =>
                    {
                        byte[] bytes   = serverClient.ByteBuffer.Dequeue(count);
                        string message = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
                        Console.WriteLine("Server client: received: " + message);

                        bytes = Encoding.UTF8.GetBytes("You said: " + message);
                        await serverClient.Send(new ArraySegment <byte>(bytes, 0, bytes.Length));

                        if (message == "bye")
                        {
                            // Let the server close the connection
                            serverClient.Disconnect();
                        }
                    }
                }.RunAsync()
            };

            server.Message += (s, a) => Console.WriteLine("Server: " + a.Message);
            var serverTask = server.RunAsync();

            var client = new AsyncTcpClient
            {
                IPAddress = IPAddress.IPv6Loopback,
                Port      = port,
                //AutoReconnect = true,
                ConnectedCallback = async(c, isReconnected) =>
                {
                    await c.WaitAsync();                       // Wait for server banner

                    await Task.Delay(50);                      // Let the banner land in the console window

                    Console.WriteLine("Client: type a message at the prompt, or empty to quit (server shutdown in 10s)");
                    while (true)
                    {
                        Console.Write("> ");
                        var consoleReadCts  = new CancellationTokenSource();
                        var consoleReadTask = ConsoleEx.ReadLineAsync(consoleReadCts.Token);

                        // Wait for user input or closed connection
                        var completedTask = await Task.WhenAny(consoleReadTask, c.ClosedTask);

                        if (completedTask == c.ClosedTask)
                        {
                            // Closed connection
                            consoleReadCts.Cancel();
                            break;
                        }

                        // User input
                        string enteredMessage = await consoleReadTask;
                        if (enteredMessage == "")
                        {
                            // Close the client connection
                            c.Disconnect();
                            break;
                        }
                        byte[] bytes = Encoding.UTF8.GetBytes(enteredMessage);
                        await c.Send(new ArraySegment <byte>(bytes, 0, bytes.Length));

                        // Wait for server response or closed connection
                        await c.ByteBuffer.WaitAsync();

                        if (c.IsClosing)
                        {
                            break;
                        }
                    }
                    // NOTE: The client connection will NOT be closed automatically when this method
                    //       returns. It has to be closed explicitly when desired.
                },
                ReceivedCallback = (c, count) =>
                {
                    byte[] bytes   = c.ByteBuffer.Dequeue(count);
                    string message = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
                    Console.WriteLine("Client: received: " + message);
                    return(Task.CompletedTask);
                }
            };

            client.Message += (s, a) => Console.WriteLine("Client: " + a.Message);
            var clientTask = client.RunAsync();

            await Task.Delay(10 * 1000);

            Console.WriteLine("Program: stopping server");
            server.Stop(true);
            await serverTask;

            client.Dispose();
            await clientTask;
        }