■
「WMIネタはどうなったんだ」という突っ込みが某所からありましたが、それには答えず「Remotingネタのプログラムを見せて」というリクエストに答えてしまうという快挙*1。
大本のネタはMSDN「Visual Basic .NETを使用してリモートサーバーを作成する方法」(=http://support.microsoft.com/default.aspx?scid=kb;ja;300951)と「Visual Basic .NETを使用してリモートサーバーにアクセスするクライアントを作成する方法」(=http://support.microsoft.com/default.aspx?scid=kb;ja;300943)なんだけど、これをVS.NET 2003を使わずに行なう。
まず、リモート可能なクラスの作成。
内容は送信された文字列とその文字数を返すだけの機能を実装している。
Microsoft.VisualBasicをImportsしているのは「Len」を使用しているため。VS.NET 2003だと、Importsしなくても使えるんだけど、VBCでコンパイルするとエラーが出たので、とりあえず入れてある。
Option Explicit
Option StrictImports System
Imports Microsoft.VisualBasicnamespace ServerClass
Public Class myRemoteClass
Inherits MarshalByRefObjectPublic Function SetString(sTemp As String) As Boolean
Try
Console.WriteLine("This string '{0}' has a length of {1}", _
sTemp, Len(sTemp))
Return (sTemp <> "")
Catch
Return (False)
End Try
End Function
End Class
End namespace
このクラスの名前空間は「ServerClass」。これを設定しておかないと、次に作る実行ファイルから参照できない。あと、リモーティングのお約束として、作成するクラスは「MarshalByRefObject」から派生させること。
コマンドラインは以下のとおり。DLLを作成するのでターゲットは「library」。
続いて、このServerClass.dllを使用する実行ファイルを作成する。これはサーバー側で起動し、クライアントから呼び出されるのを待ち続ける。プロトコルは「Tcp」を使用しポートは「8085」固定にして「ChannelServices.RegisterChannel」で登録。もちろん「Http」を使って「80」ポートをリスンし続け、なんか話しかけてきたら返すようなアプリケーションにしてもいい。その代わり先に作ったクラスも作り替えナイト。
> vbc /t:library /out:ServerClass /r:system.dll /debug+.vb
先ほど作ったリモート可能なクラス「SeverClass.myRemoteClass」を「RemotingConfiguration.RegisterWellKnownServiceType」で登録。
名前空間「SeverClass」の機能を呼び出すだけのコンソール型アプリケーションなので、「Module」を使用している。
Option Explicit
Option StrictImports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports ServerClassModule ServerApp
Sub Main(ByVal cmdArgs() As String)
Dim chan As TcpChannel = New TcpChannel(8085)
ChannelServices.RegisterChannel(chan)RemotingConfiguration.RegisterWellKnownServiceType( _
Type.GetType("ServerClass.myRemoteClass, ServerClass"), _
"RemoteTest", WellKnownObjectMode.SingleCall)
Console.WriteLine("Pressto exit...")
Console.ReadLine()
End Sub
End Module
コンパイルオプションは以下のとおり。
これで「SeverClass.dll」と「ServerApp.exe」が完成したので、今度はクライアント側のアプリケーションを作る。
> vbc /t:exe /out:ServerApp /r:system.dll,system.runtime.remoting.dll,severclass.dll /debug+.vb
こちらもコンソールアプリケーションとして作成。
Option Explicit
Option StrictImports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports ServerClassModule hoge
Sub Main(ByVal cmdArgs() As String)
Dim chan As TcpChannel = New TcpChannel()
ChannelServices.RegisterChannel(chan)Dim obj As myRemoteClass
obj = CType(Activator.GetObject( _
Type.GetType("ServerClass.myRemoteClass, ServerClass"), _
"tcp://localhost:8085/RemoteTest"), myRemoteClass)If obj Is Nothing Then
Console.WriteLine("Could not locate server.")
Else
If obj.SetString("Sending String to Server") Then
Console.WriteLine("Success: Check the other console to verify.")
Else
Console.WriteLine("Sending the Test string has failed.")
End If
End IfConsole.WriteLine("Press
to exit...")
Console.ReadLine()
End Sub
End Module
こちらも「ChannelServices.RegisterChannel」でチャンネル(プロトコル:Tcp、ポート:8085)を登録し、「ServerClass」クラスを実体化させる。
実体化が成功したら「Sending String to Server」をサーバーに送り(これがServerClassで評価される)、失敗したら「サーバーが見つからない」あるいは「文字列を送れなかった」旨を表示して終了。
コンパイルオプションは以下のとおり。
「ClientApp.exe」が出来上がったら、まず、「ServerApp」を起動する。
> vbc /t:exe /out:ClientApp /r:system.dll,system.Runtime.Remoting.dll,ServerClass.dll /debug+.vb
となったら、もうひとつコマンドプロンプトを立ち上げ、「ClientApp.exe」を実行する。クライアント側に、
C:\REMOTE_TEST> ServerApp.exe
Pressto exit...
Success: Check the other console to verify.と表示され、サーバー側には、
Pressto exit...
と表示される。
This String 'Sending String to Server' has a length of 24