命名管道不适用于异步双工通信

时间:2018-06-16 18:02:53

标签: c# asynchronous named-pipes duplex

不知怎的,我无法让命名管道在.Net应用程序的客户端和服务器之间进行双工通信。 当我进行顺序消息传递时,双工通信工作正常,但是当我随机发送它时,服务器和客户端可以在任何随机时间互相ping通,它就不起作用。

其他帖子也没有多大帮助 - c# Full Duplex Asynchronous Named Pipes .NET

我附上我的代码如下:

- 服务器代码:

            namespace Client
            {
                class Program
                {
                    static void Main(string[] args)
                    {
                        var namedPipeClientStream = new NamedPipeClientStream(".", "server", PipeDirection.InOut, PipeOptions.Asynchronous,
                                            TokenImpersonationLevel.Impersonation);
                        Task.Run(() => StartListeningAsync(namedPipeClientStream, (msg) => Console.WriteLine(msg)));
                        Task.Run(() => SendMessageAsync(namedPipeClientStream));
                        Console.ReadLine();
                    }

                    public static async Task SendMessageAsync(NamedPipeClientStream namedPipeClient)
                    {
                        using (var stream = new StreamWriter(namedPipeClient))
                        {
                            while (true)
                            {
                                try
                                {
                                    await Task.Delay(3000);
                                    var serialized = JsonConvert.SerializeObject($"Client {DateTime.Now}");
                                    byte[] messageBytes = Encoding.UTF8.GetBytes(serialized);
                                    if (!namedPipeClient.IsConnected)
                                    {
                                        namedPipeClient.Connect();
                                        namedPipeClient.ReadMode = PipeTransmissionMode.Message;
                                        Console.WriteLine("Client connected");
                                    }
                                    await namedPipeClient.WriteAsync(messageBytes, 0, messageBytes.Length);
                                    await namedPipeClient.FlushAsync();
                                    namedPipeClient.WaitForPipeDrain();
                                }
                                catch (Exception exception)
                                {
                                    Console.WriteLine($"Exception:{exception}");
                                }
                            }
                        }
                    }

                    public static async Task StartListeningAsync(NamedPipeClientStream namedPipeClient, Action<string> messageRecieved)
                    {
                        using (var streamReader = new StreamReader(namedPipeClient))
                        {
                            while (true)
                            {
                                try
                                {

                                    StringBuilder messageBuilder = new StringBuilder();
                                    string messageChunk = string.Empty;
                                    byte[] messageBuffer = new byte[1024];
                                    do
                                    {
                                        if (!namedPipeClient.IsConnected)
                                        {
                                            namedPipeClient.Connect();
                                            namedPipeClient.ReadMode = PipeTransmissionMode.Message;
                                        }
                                        await namedPipeClient.ReadAsync(messageBuffer, 0, messageBuffer.Length);
                                        messageChunk = Encoding.UTF8.GetString(messageBuffer);
                                        messageBuilder.Append(messageChunk);
                                        messageBuffer = new byte[messageBuffer.Length];
                                    } while (!namedPipeClient.IsMessageComplete);
                                    if (messageRecieved != null)
                                    {
                                        messageRecieved(JsonConvert.DeserializeObject<string>(messageBuilder.ToString()));
                                    }
                                }
                                catch (Exception exception)
                                {
                                    Console.WriteLine($"Exception:{exception}");
                                }
                            }
                        }
                    }
                }
            }

客户代码:

{{1}}

0 个答案:

没有答案