2007年5月11日金曜日

Ipcプロセス通信テスト

参考リンク
http://www.microsoft.com/japan/msdn/netframework/skillup/core/article1.aspx
http://msdn2.microsoft.com/ja-jp/library/system.runtime.remoting.channels.ipc.ipcserverchannel(VS.80).aspx
http://msdn2.microsoft.com/ja-jp/library/system.runtime.remoting.channels.ipc.ipcclientchannel(VS.80).aspx



.NET 2.0から
参照設定としてクライアントとサーバーに以下の物が必要
System.Runtime.Remoting.Channels
リモートオブジェクトのプロジェクト、またはdllの参照




---------------------



サーバーコード



using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;



public class IpcServer
{



    public static void Main ()
    {
        // Create and register an IPC channel
        IpcServerChannel serverChannel = new IpcServerChannel("remote");
        ChannelServices.RegisterChannel(serverChannel);



        // Expose an object
        RemotingConfiguration.RegisterWellKnownServiceType( typeof(Counter),
"counter", WellKnownObjectMode.Singleton );



        // Wait for calls
        Console.WriteLine("Listening on {0}",
serverChannel.GetChannelUri());
        Console.ReadLine();
    }



}




---------------------



クライアントコード



using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;



public class Client
{
    public static void Main ()
    {
        IpcClientChannel clientChannel = new IpcClientChannel();
        ChannelServices.RegisterChannel(clientChannel);



        RemotingConfiguration.RegisterWellKnownClientType( typeof(Counter) ,
"ipc://remote/counter" );



        Counter counter = new Counter();
        Console.WriteLine("This is call number {0}.", counter.Count);
    }



}




---------------------



リモートオブジェクト



using System;



public class Counter : MarshalByRefObject {



  private int count = 0;



  public int Count { get {
    return(count++);
  } }



}



0 件のコメント:

コメントを投稿