VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > C#编程 >
  • C#教程之Named pipe Stream include NamedPipeServerStream and NamedPipeClientStream

本站最新发布   C#从入门到精通
试听地址  
https://www.xin3721.com/eschool/CSharpxin3721/

Named pipe (more flexible)
Allows two-way communication between arbitrary processes on the same computer or different computers across a network.A pipe is good for interprocess communication (IPC) on a single computer: it doesn’t rely on a network transport, which means no network protocol overhead, and it has no issues with firewalls.

 

Server:

复制代码
static NamedPipeServerStream serverStream;
        static int i = 0;
        static void NamedPipeServerStreamDemo()
        {
            serverStream = new NamedPipeServerStream("FredPipeServerStream");
            serverStream.WaitForConnection();           
            System.Timers.Timer timer = new System.Timers.Timer(100);
            timer.Elapsed += Timer_Elapsed;
            timer.Start();                 
        }

        private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            string str = $"i is {i},now is {DateTime.Now.ToString("yyyyMMddHHmmssffff")},Guid is {Guid.NewGuid()}";
            byte[] serverBytes = Encoding.UTF8.GetBytes(str);
            serverStream.Write(serverBytes, 0, serverBytes.Length);
            i++;
        }
复制代码

Client:

复制代码
static void NamedPipeClientStreamDemo()
        {
            var clientStream = new NamedPipeClientStream("FredPipeServerStream");
            clientStream.Connect();
            while(true)
            {
                byte[] receiveData = new byte[100];
                int result = clientStream.Read(receiveData, 0, receiveData.Length);
                string str = Encoding.UTF8.GetString(receiveData);
                Console.WriteLine($"Client receive {str}");
            }           
        }
复制代码

相关教程