#region  客户端
        NamedPipeClientStream pipeClient =
        new NamedPipeClientStream("localhost", "testpipe", PipeDirection.InOut, PipeOptions.Asynchronous, TokenImpersonationLevel.None);
        StreamWriter sw = null;

private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                pipeClient.Connect(5000);
                sw = new StreamWriter(pipeClient);
                sw.AutoFlush = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("连接建立失败,请确保服务端程序已经被打开。");
                this.Close();
            }  
        }

private void button1_Click(object sender, EventArgs e)
        {
            if (sw != null)
            {
                sw.WriteLine(this.richTextBox1.Text);
            }
            else
            {
                MessageBox.Show("未建立连接,不能发送消息。");
            }
        }

#endregion

#region  服务端

NamedPipeServerStream pipeServer =
          new NamedPipeServerStream("testpipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous);

private void Form1_Load(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(delegate
            {
                pipeServer.BeginWaitForConnection((o) =>
                {
                    NamedPipeServerStream pServer = (NamedPipeServerStream)o.AsyncState;
                    pServer.EndWaitForConnection(o);
                    StreamReader sr = new StreamReader(pServer);
                    while (true)
                    {
                        this.Invoke((MethodInvoker)delegate { listView1.Items.Add(sr.ReadLine()); });
                    }
                }, pipeServer);
            });
        }

#endregion

最新文章

  1. iOS网络推送消息
  2. 手动部署servlet
  3. You and Your Research(Chinese)
  4. Java系列:Add Microsoft SQL JDBC driver to Maven
  5. cocos2d ccmenulabel
  6. saltstack之(十一)扩展组件salt-returners
  7. 161025、java提高篇之关键字static
  8. Cocos2D-x权威指南:通过节点控制屏幕中的全体渲染对象
  9. C#中的DateTime:本周第一天,本月第一天,今年第一天,本周第一天的时间
  10. UML建模之状态图(Statechart Diagram)
  11. CSS样式----CSS的继承性和层叠性(图文详解)
  12. DSAPI 生成桌面图标(带数字)
  13. 从壹开始前后端分离 [.netCore 填坑 ] 三十三║ ⅖ 种方法实现完美跨域
  14. 2018-2019-2 网络对抗技术 20165225 Exp6 信息搜集与漏洞扫描
  15. 【转】Mybatis源码解读-设计模式总结
  16. 点击导出table表格
  17. db2 OLAP函数使用
  18. 51nod 1069 Nim游戏 + BZOJ 1022: [SHOI2008]小约翰的游戏John(Nim游戏和Anti-Nim游戏)
  19. Java判断对象是否为NULL
  20. HTML - 网页特殊字符大全(转)

热门文章

  1. [python] super() 用法
  2. 集合运算 - Java实现集合的交、并、差
  3. 为什么 ConcurrentHashMap 的读操作不需要加锁?
  4. C++学习 之 类中的特殊函数和this指针(笔记)
  5. js文件的框架
  6. softmax函数笔记
  7. 深入理解hive基础学习
  8. SSM处理 No 'Access-Control-Allow-Origin' header is present on the requested resource 问题
  9. python cv2读取rtsp实时码流按时生成连续视频文件
  10. Windows访问VirtualBox的Redis服务器