国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#中用NamedPipe進程間通信

2019-11-17 04:20:06
字體:
來源:轉載
供稿:網友

本文只是一個測試例子,核心代碼是kernel32.dll中的一組windows api函數,這里不深入研究,代碼都在codePRoject上。

http://www.codeproject.com/KB/threads/dotnetnamedpipespart1.aspx

測試效果如下,可以做到aspx和給console app發送消息后得到反饋:

console app為服務器端代碼如下

view plaincopy to clipboardprint?
using System;  
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();  
        }  
    }  

using System;
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();
        }
    }
}
 

 

客戶端的aspx代碼如下

view plaincopy to clipboardprint?
using System;  
using System.Web;  
using AppModule.InterProcessComm;  
using AppModule.NamedPipes;  
public partial class _Default : System.Web.UI.Page   
{  
    protected void Page_Load(object sender, EventArgs e)  
    {  
        Response.Write(SendRequest("測試asdf"));  
    }  
    /// <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", ".");  
            clientConnection.Connect();  
            clientConnection.Write(request);  
            response=clientConnection.Read();  
            clientConnection.Close();  
        }  
        catch (Exception ex)  
        {  
            clientConnection.Dispose();  
            response = ex.Message;  
        }  
        return response;  
    }  

using System;
using System.Web;
using AppModule.InterProcessComm;
using AppModule.NamedPipes;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(SendRequest("測試asdf"));
    }
    /// <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", ".");
            clientConnection.Connect();
            clientConnection.Write(request);
            response=clientConnection.Read();
            clientConnection.Close();
        }
        catch (Exception ex)
        {
            clientConnection.Dispose();
            response = ex.Message;
        }
        return response;
    }
}
 

 

測試環境為windows Vista和windows2003

http://blog.csdn.net/jinjazz/archive/2009/02/03/3861143.aspx


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 马龙县| 乳山市| 苏尼特左旗| 临朐县| 英德市| 汶上县| 华阴市| 青浦区| 拉孜县| 靖州| 右玉县| 福清市| 长兴县| 镇平县| 新竹市| 景泰县| 铜川市| 广南县| 孝昌县| 肃南| 晋州市| 南昌市| 泰安市| 出国| 陇南市| 正阳县| 建水县| 澳门| 三河市| 濉溪县| 封开县| 南靖县| 青州市| 永城市| 青神县| 三原县| 依兰县| 营口市| 洛宁县| 思南县| 邵阳县|