Я пытаюсь использовать NamedPipe.Я создал два консольных приложения.Один для клиента, а другой для сервера.
Код клиента:
class Program
{
static void Main(string[] args)
{
long total = 0;
Stopwatch stopWatch = new Stopwatch();
Stopwatch stopWatch2 = new Stopwatch();
stopWatch2.Start();
for (int i = 0; i < 10000; i++)
{
stopWatch.Restart();
var client = new Client()
{
Name = "Client" + i,
ClientStream = new NamedPipeClientStream("testPipe"),
};
client.ClientStream.Connect();
if (client.ClientStream.IsConnected)
{
client.Multiply(2);
var x = stopWatch.ElapsedMilliseconds;
Console.WriteLine(i + ": Time : " + x);
client.ClientStream.Dispose();
client.ClientStream.Close();
total += x;
}
}
Console.WriteLine("Total: " + stopWatch2.ElapsedMilliseconds);
Console.WriteLine("Average: " + total / 10000);
Console.ReadLine();
}
}
Код сервера:
public class PipeServer
{
public static void Main(string[] args)
{
Console.WriteLine("Waiting for client to connect:");
NamedPipeServerStream pipeServer = new NamedPipeServerStream("testPipe", PipeDirection.InOut, 4);
while (!pipeServer.IsConnected)
{
pipeServer.WaitForConnection();
Console.WriteLine("Client Connected");
try
{
if (pipeServer.IsConnected)
{
var data = pipeServer.ReadByte();
pipeServer.WriteByte((byte)(data * 2));
}
}
catch (Exception ex)
{
}
finally
{
pipeServer.Disconnect();
Console.WriteLine("Client Disconnected");
}
}
}
}
Модель клиента:
public class Client
{
public string Name { get; set; }
public NamedPipeClientStream ClientStream { get; set; }
public int Multiply(int x)
{
ClientStream.WriteByte(2);
var data = ClientStream.ReadByte();
return data;
}
}
Поэтому, когда я запускаю это приложение на Mono, оно подключается без запуска сервера.Но этот же код прекрасно работает на Windows .Net Framework 4.7.Кто-нибудь знает, почему это происходит на Mono?