简体   繁体   中英

NamedPipeClientStream does not connect

In our main application we have the following function (in 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:

 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. The first time this function is called it works correctly. But the second time the code blocks on 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. 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 之后,代码卡在循环内,不再等待客户端连接。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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