简体   繁体   English

NamedPipeClientStream 未连接

[英]NamedPipeClientStream does not connect

In our main application we have the following function (in VB.Net):在我们的主应用程序中,我们有以下功能(在 VB.Net 中):

 Public Overrides Function GetNewResults(setToProcessed As Boolean) As List(Of ReceivedData)

    Dim client = New NamedPipeClientStream(".", "PipesOfPiece",
                                           PipeDirection.InOut, PipeOptions.None,
                                           TokenImpersonationLevel.Impersonation)
    client.Connect()
    Dim reader = New StreamReader(client)
    Dim writer = New StreamWriter(client)
    writer.AutoFlush = True

    writer.WriteLine(DbPath)

    Dim serialized = reader.ReadLine
    client.Close()

    Dim newResults = JsonConvert.DeserializeObject(Of List(Of ReceivedData))(serialized)

    Return newResults

End Function

The NamedPipeServer has been written in C# and, for now, is hosted in a console application: NamedPipeServer 是用 C# 编写的,目前托管在控制台应用程序中:

 static void StartServer()
    {

        server = new NamedPipeServerStream("PipesOfPiece", direction: PipeDirection.InOut,maxNumberOfServerInstances:10);

        server.WaitForConnection();
        StreamReader reader = new StreamReader(server);
        StreamWriter writer=  new StreamWriter(server);
        writer.AutoFlush = true;

        while (true)
            {
                var dbPath = reader.ReadLine();
                if (string.IsNullOrEmpty(dbPath)) continue;
                if (!File.Exists(dbPath))
                {
                    writer.WriteLine($"{dbPath} does not exist.");
                    continue;
                }
                var communicator = new DapperBwsCommunicator(dbPath,provider: "Microsoft.Jet.OLEDB.4.0");
                var newResults = communicator.GetAllResults(setToProcessed: true);
                var serialized = JsonConvert.SerializeObject(newResults);
                writer.WriteLine(serialized);
            }
    }

The GetNewResults function is called every three seconds. GetNewResults函数每三秒调用一次。 The first time this function is called it works correctly.第一次调用此函数时,它可以正常工作。 But the second time the code blocks on Client.Connect .但是第二次代码在Client.Connect阻塞。 It seems then that the server does not respond any more.服务器似乎不再响应。

Where am I going wrong?我哪里错了?

EDIT Thanks to Fandango's answer I came with a different implementation.编辑感谢 Fandango 的回答,我带来了不同的实现。 This seems to work:这似乎有效:

static void StartServer()
{

    server = new NamedPipeServerStream("PipesOfPiece", direction: PipeDirection.InOut,maxNumberOfServerInstances:10);

    server.WaitForConnection();
    StreamReader reader = new StreamReader(server);
    StreamWriter writer=  new StreamWriter(server);
    writer.AutoFlush = true;

    while (true)
        {
            var dbPath = reader.ReadLine();
            if (string.IsNullOrEmpty(dbPath)) continue;
            if (!File.Exists(dbPath))
            {
                writer.WriteLine($"{dbPath} does not exist.");
                continue;
            }
            var communicator = new DapperBwsCommunicator(dbPath,provider: "Microsoft.Jet.OLEDB.4.0");
            var newResults = communicator.GetAllResults(setToProcessed: true);
            var serialized = JsonConvert.SerializeObject(newResults);
            writer.WriteLine(serialized);
            server.Disconnnect();
            server.WaitForConnection();
        }
}

发生这种情况是因为您每 3 秒不断创建一个新的客户端连接,而在服务器端,在第一个 WaitForConnection 之后,代码卡在循环内,不再等待客户端连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM