Я делаю сервис Windows в C#, и мне нужно общаться с этой службой, используя ждемчик natedPipeserverStream. Когда я начинаю эту услугу, подключение к ней без каких -либо проблем. Когда я закрываю клиента и снова открываю
, я не могу воссоединиться с этой службой. Где проблема? < /P>
Вот код сервера: < /p>
private async Task StartPipeServer()
{
while (true)
{
PipeSecurity pipeSecurity = new PipeSecurity();
pipeSecurity.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), PipeAccessRights.ReadWrite, AccessControlType.Allow));
using (var pipeServer = new NamedPipeServerStream("MyPipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous, 1024, 1024, pipeSecurity))
{
await pipeServer.WaitForConnectionAsync();
using (StreamReader reader = new StreamReader(pipeServer, Encoding.UTF8))
using (StreamWriter writer = new StreamWriter(pipeServer, Encoding.UTF8) { AutoFlush = true })
{
while (pipeServer.IsConnected)
{
string message = await reader.ReadLineAsync();
string[] CastiPrikazu = message.Split(' ');
switch (CastiPrikazu[0])
{
case "Ahoj":
writer.WriteLine("ahoj");
break;
}
}
}
}
}
}
< /code>
Вот клиент -код: < /p>
class Program
{
static async Task Main(string[] args)
{
using (var client = new NamedPipeClientStream(".", "MyPipe", PipeDirection.InOut))
{
Console.WriteLine("Connecting to service...");
await client.ConnectAsync();
using (StreamWriter writer = new StreamWriter(client, Encoding.UTF8) { AutoFlush = true })
using (StreamReader reader = new StreamReader(client, Encoding.UTF8))
{
while (true)
{
Console.Write("Type Message: ");
string message = Console.ReadLine();
await writer.WriteLineAsync(message);
string response = await reader.ReadLineAsync();
Console.WriteLine($"[ANSWER]: {response}");
}
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... rverstream
Как воссоединиться с названным ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение