我是第一次使用命名管道.我的最终目标是让一个既能充当客户端、服务器又能独立工作的可执行文件.服务器可以具有提升的权限.
我对来回转接有困难.首先,除第一次外,每次写回客户端时都以‘?’开头.互联网上说这是一个编码问题,但它是同一个函数(WriteClient)正在做这件事,所以我不能确定为什么第一次会不同.(打印输出也证实了作者和读者每次都说了相同的编码)第二,我想确保我适当地关闭了连接.它花了一些试错的时间.
Readout from Client
In this version, I told it to print the encoding as well to confirm it didn't change.
Connected.
Version Installed: 1.2.8
?Latest Version: 1.2.85
?Update Needed
?Run again without '-Check' to perform a silent installation.
?Close
Connection closed.
100
using System;
using System.Diagnostics;
using System.Xml.Linq;
using System.Runtime.Versioning;
using System.IO.Pipes;
using System.Text;
using System.Net;
using System.ServiceProcess;
using System.Security.Authentication.ExtendedProtection;
[SupportedOSPlatform("windows")]
class DAQUpdate
{
static void Main(string[] args)
{
if (args.Length > 0)
{
//Check if the same program is running as a service. If so, use that as it will have elevated previleges
try
{
using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "DAQUpdater", PipeDirection.InOut))
{
//Connect to the service with a 1 second timeout. This will return an error on timeout
pipeClient.Connect(1);
//Double check if connected for reasons.
if(pipeClient.IsConnected)
{
Console.WriteLine("Connected.");
// Send the args to the server
using (StreamWriter writer = new StreamWriter(pipeClient,Encoding.UTF8,-1,true))
{
//Packages the args with ; to separate since I can't pass objects
writer.WriteLine(string.Join(';', args));
writer.Flush();
}
//Read all responses from the server as it gives them. The final response will be Close
string response = "";
using (StreamReader reader = new StreamReader(pipeClient, Encoding.UTF8))
{
while(!response.Contains("Close"))
{
response = reader.ReadLine();
Console.WriteLine(response);
}
pipeClient.Close();
}
Console.WriteLine("Connection closed.");
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("No Connection.");
}
}
else
{
NamedPipeServer.runServer();
}
}
public static void update(string[] args)
{
NamedPipeServer.writeClient("hello");
NamedPipeServer.writeClient(args[0]);
NamedPipeServer.writeClient("Testing");
NamedPipeServer.writeClient("Close");
}
}
Server Functions个
// NamedPipeServer.cs
using System.IO.Pipes;
using System.Text;
public class NamedPipeServer
{
private static NamedPipeServerStream pipeServer;
public static void runServer()
{
try
{
while(true)
{
//Changed to global so other functions can call writeClient and send messages to the client from anywhere
pipeServer = new NamedPipeServerStream("DAQUpdater", PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
{
Console.WriteLine("Waiting for connection...");
pipeServer.WaitForConnection();
Console.WriteLine("Client connected.");
// Read and process the command from the client
using (StreamReader reader = new StreamReader(pipeServer, Encoding.UTF8,true,-1,true ))
{
string command = reader.ReadLine();
string[] commandargs = command.Split(';');
Console.WriteLine($"Received command: {commandargs[0]}");
//Run the check for updates program which will also write to the client
DAQUpdate.update(commandargs);
// Send a response back to the client if needed
using (StreamWriter writer = new StreamWriter(pipeServer, Encoding.UTF8, -1 , true))
{
writer.WriteLine("Close");
writer.Flush();
}
}
pipeServer.Close();
Console.WriteLine("Connection closed.");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
public static void writeClient(string messageTxt)
{
//allows other functions to send update messages to the client
if(pipeServer != null)
{
if(pipeServer.IsConnected)
{
using (StreamWriter writer = new StreamWriter(pipeServer, Encoding.UTF8, -1 , true))
{
Console.WriteLine(writer.Encoding);
writer.WriteLine(messageTxt);
writer.Flush();
}
}
}
}
}