天天看點

C#有名管道的程序間通信

程序間通信包括:有名管道,無名管道,信号量,消息隊列,共享記憶體以及socket。

有名管道可以用于任意程序間的通信,而無名管道必須是有血緣關系程序間的通信。

1.服務端

using System;

using System.Collections.Generic;

using System.IO;

using System.IO.Pipes;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace PipeServer

{

    class Program

    {

        static void Main(string[] args)

        {

            WaitData();

        }

        private static void WaitData()

        {

            using (NamedPipeServerStream pipeserver = new NamedPipeServerStream("TestPipe", PipeDirection.InOut, 5))

            {

                try

                {

                    Console.WriteLine("Wait Connect");

                    pipeserver.WaitForConnection();//等待用戶端連接配接

                    pipeserver.ReadMode = PipeTransmissionMode.Byte;//讀取模式為位元組

                    Console.WriteLine("success connected?"+pipeserver.IsConnected.ToString());

                    StreamReader sr = new StreamReader(pipeserver);//擷取管道輸入流

                    StreamWriter sw = new StreamWriter(pipeserver);//擷取管道輸出流

                    string result = string.Empty;

                    while (true)

                    {

                        result = sr.ReadLine();

                        if (result == null || result == "bye")

                        {

                            break;

                        }

                        else

                        {

                            Console.WriteLine(result);

                            sw.WriteLine("I am Server.");//發送給用戶端

                            sw.Flush();//清空緩存以免管道阻塞

                        }

                    }

                    Console.ReadKey();

                }

                catch (IOException ee)

                {

                    throw ee;

                }

            }

        }

    }

}

//--------------------------------------------------------------//

2.用戶端

using System;

using System.Collections.Generic;

using System.IO;

using System.IO.Pipes;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace PipClient

{

    class Program

    {

        static void Main(string[] args)

        {

            StartClient();

            Console.ReadKey();

        }

        private static void StartClient()

        {

            string input = string.Empty;

            try

            {

                using (NamedPipeClientStream pipeStream = new NamedPipeClientStream("localhost", "TestPipe"))

                {

                    pipeStream.Connect();//連接配接服務端

                    if (!pipeStream.IsConnected)

                    {

                        Console.WriteLine("Failed to connect ....");

                        return;

                    }

                    StreamWriter sw = new StreamWriter(pipeStream);//擷取管道輸出流

                    StreamReader sr = new StreamReader(pipeStream);//擷取管道輸入流

                    while (true)

                    {

                        input = Console.ReadLine();

                        Console.WriteLine("SendMessage"+input);

                        sw.WriteLine(input);//發送消息給服務端

                        sw.Flush();//清空緩存

                        string temp = string.Empty;

                        temp = sr.ReadLine();//擷取服務端響應的消息

                        Console.WriteLine("reply Content:"+temp);

                    }

                }

            }

            catch (IOException ee)

            {

                Console.WriteLine(ee.Message.ToString()+"\n"+ee.StackTrace.ToString());

                throw ee;

            }

        }

    }

}