0

I am trying to use NamedPipe. I have created two console based application. One is for client and another is for server.

Client Code:

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();
    }
}

Server Code:

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");
            }
        }
    }
}

Client Model:

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;
    }
}

So when I run this application on Mono it connects without starting server. But this same code works perfectly on windows .Net Framework 4.7. Is anyone aware why does this happens on Mono?

adarsh
  • 31
  • 6
  • that empty `catch` is making my eyes bleed, are you sure you're not swallowing some exception by mistake here? – knocte Nov 29 '18 at 03:01
  • Thet empty catch is in Server Code. I am not running Server code. The client code is a different console based application. It should not execute without the server. But it executes without the server. On .NET Framework on Windows, it works fine. – adarsh Dec 04 '18 at 08:32

0 Answers0