IpcServerChannel

IpcServerChannelは、IPCというプロセス間通信のサーバチャンネルを開くためのクラス・・・。

IpcServerChannel クラス (System.Runtime.Remoting.Channels.Ipc)

IPCサーバとIPCクライアント間でIPCプロセス間通信をしてみる・・・。

f:id:BG1:20170322133637p:plain

今回はWindowsアプリケーションでまずサーバを作る・・・。

f:id:BG1:20170322133735p:plain

参照を追加・・・。

f:id:BG1:20170322133756p:plain

System.Runtime.Remotingが必要・・・。

IpcServerChannel_プロジェクトのForm1.csは、

System.Runtime.Remoting以下をusing・・・。
IpcServerChannelオブジェクトserverをChannelServices.RegisterChannelで登録・・・。
後述するRemoteObjectのremoteObjをRemotingServices.Marshalで公開する流れ・・・。
今回は、server生成時にポート名"ipcserver"、公開時にURI"message"と指定しているので"ipc://ipcserver/message"というURIで公開される・・・。

f:id:BG1:20170322134554p:plain

新しいプロジェクトを追加・・・。

f:id:BG1:20170322134617p:plain

今度はIpcClientChannel_・・・。
こちらにもSystem.Runtime.Remotingの参照を追加しておく・・・。
そして、

clientをChannelServices.RegisterChannelに登録・・・。

f:id:BG1:20170322134938p:plain

サーバもクライアントもテキストボックスとボタンを配置・・・。

f:id:BG1:20170322135034p:plain

IpcClientChannel_では、タイトルをClientに・・・。
IpcServerChannel_では、タイトルをServerに・・・。

あとは、ボタンのハンドラ・・・。

サーバはボタンを押したら、remoteObj.MessageにtextBox1.Textの内容を渡す・・・。

クライアントはActivator.GetObjectに"ipc://ipcserver/message"を指定してremoteObjを取る・・・。
取ったremoteObj.MessageをtextBox1.Textに・・・。

でこのRemoteObjectは、

と、

両方のプロジェクトで定義・・・。
名前空間も共通化して見えるように・・・。

f:id:BG1:20170322135805p:plain

Serverで"ABCDE"と入力して、button1を押す・・・。
Client側は空の状態でbutton1を押すと、

f:id:BG1:20170322135822p:plain

反映される・・・。

f:id:BG1:20170322135848p:plain

Serverで文字列を変えて、button1を押し、
Client側でbutton1を押すと、

f:id:BG1:20170322135923p:plain

また反映される・・・。

Sample/dotnet/IpcServerChannel/IpcServerChannel/src/IpcServerChannel_ at master · bg1bgst333/Sample · GitHub