日期:2014-05-18 浏览次数:20470 次
protected void Page_Load(object sender, EventArgs e) { IInterProcessConnection clientConnection = null; try { clientConnection = new ClientPipeConnection("MyPipe", "."); clientConnection.Connect(); clientConnection.Write("aa"); Response.Write(clientConnection.Read()); clientConnection.Close(); } catch (Exception ex) { clientConnection.Dispose(); Response.Write (ex.Message); } }
------解决方案--------------------
跟后台程序通讯最简单的办法还是通过database来做比较稳妥。
------解决方案--------------------
using System; using System.Collections.Generic; using System.Text; using AppModule.InterProcessComm; using AppModule.NamedPipes; using System.Threading; namespace Server { class Program { //**c#中用namedpipe进程间通信 //**组件代码来自codeproject //**http://www.codeproject.com/KB/threads/dotnetnamedpipespart1.aspx //**下载上面链接中的代码,编译AppModule.InterProcessComm和AppModule.NamedPipes两个dll //**引用这两个dll到本例中,运行如下代码作为服务器端测试 //**测试代码by jinjazz(因为原作者的两个测试程序比较复杂,这里简化后供大家参考) static void Main(string[] args) { ServerPipeConnection PipeConnection = new ServerPipeConnection("np-test-by-jinjazz", 512, 512, 5000, false); Console.WriteLine("listening.."); while (true) { try { PipeConnection.Disconnect(); PipeConnection.Connect(); string request = PipeConnection.Read(); if (!string.IsNullOrEmpty(request)) { Console.WriteLine("get:" + request); PipeConnection.Write("get:" + request); if (request.ToLower() == "break") break; } } catch (Exception ex) { Console.WriteLine(ex.Message); break; } } PipeConnection.Dispose(); Console.Write("press any key to exit.."); Console.Read(); } //**客户端测试代码如下,可以是应用程序或者web程序 #if false /// <summary> /// 测试namepiped客户端 /// </summary> /// <param name="request">发送命令</param> /// <returns>返回数据</returns> string SendRequest(string request) { string response=""; IInterProcessConnection clientConnection = null; try { clientConnection = new ClientPipeConnection("np-test-by-jinjazz", "10.10.1.57"); clientConnection.Connect(); clientConnection.Write(request); response=clientConnection.Read(); clientConnection.Close(); } catch (Exception ex) { clientConnection.Dispose(); response