Name Pipe

Pipe的处理在windows中是在System.IO.Popes 类中,在linux是在Mono.Unix.UnixPipes

Windows中只有.net3.5之后才有处理pipe的信息

 

Namepipe的代码主要使用NamedPipeServerStream NamedPipeClientStream,下面是例子:

using System;

using System.IO;

using System.IO.Pipes;

using System.Threading;

 

namespace PipeApplication1

{

    class ProgramPipeTest

    {

 

        public void ThreadStartServer()

        {

            // Create a name pipe

            using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("mytestpipe"))

            {

                Console.WriteLine("[Server] Pipe created {0}", pipeStream.GetHashCode());

 

                // Wait for a connection

                pipeStream.WaitForConnection();

                Console.WriteLine("[Server] Pipe connection established");

 

                using (StreamReader sr = new StreamReader(pipeStream))

                {

                    string temp;

                    // We read a line from the pipe and print it together with the current time

                    while ((temp = sr.ReadLine()) != null)

                    {

                        Console.WriteLine("{0}: {1}", DateTime.Now, temp);

                    }

                }

            }

 

            Console.WriteLine("Connection lost");

        }

 

        public void ThreadStartClient(object obj)

        {

            // Ensure that we only start the client after the server has created the pipe

            ManualResetEvent SyncClientServer = (ManualResetEvent)obj;

 

            // Only continue after the server was created -- otherwise we just fail badly

            // SyncClientServer.WaitOne();

 

            using (NamedPipeClientStream pipeStream = new NamedPipeClientStream("mytestpipe"))

            {

                // The connect function will indefinately wait for the pipe to become available

                // If that is not acceptable specify a maximum waiting time (in ms)

                pipeStream.Connect();

 

                Console.WriteLine("[Client] Pipe connection established");

                using (StreamWriter sw = new StreamWriter(pipeStream))

                {

                    sw.AutoFlush = true;

                    string temp;

                    Console.WriteLine("Please type a message and press [Enter], or type 'quit' to exit the program");

                    while ((temp = Console.ReadLine()) != null)

                    {

                        if (temp == "quit") break;

                        sw.WriteLine(temp);

                    }

                }

            }

        }

 

        static void Main(string[] args)

        {

 

            // To simplify debugging we are going to create just one process, and have two tasks

            // talk to each other. (Which is a bit like me sending an e-mail to my co-workers)

 

            ProgramPipeTest Server = new ProgramPipeTest();

            ProgramPipeTest Client = new ProgramPipeTest();

 

            Thread ServerThread = new Thread( Server.ThreadStartServer );

            Thread ClientThread = new Thread(Client.ThreadStartClient);

 

            ServerThread.Start();

            ClientThread.Start();

        }

    }

}

 

 

C#Boolean4byte

 

byte[] aa= byte[0]

C#中是成立的,此时不分配空间。

GUID128bit

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值