Remoting has at least three sections :
Common Interface which will be shared between them.
Server.
Client.
Here is an explanation of the process:
First important section is the common interface between Server and Client. For sample project interface
is very simple with only two methods : SetValue and GetValue.
is very simple with only two methods : SetValue and GetValue.
Public Interface InterFaceRemotingSub SetValue(ByVal value As String)Function GetValue() As StringEnd Interface Second important section is the server. In this sample server is using HTTP channel and the server object is singleton.
In the code above, Channel object is created and registered. Server then hosts the object so that client can connect to it. This is the time when we specify what mode the server object will be created i.e. Singleton or SingleCall. This is done by the following below given code. Note in sample we are hosting the server object in singleton mode that means that the same object
will be shared between all clients. Also note the server object is implementing "InterFaceRemoting" and inheriting from "MarshalByRefObject".
Imports SystemImports System.Runtime.RemotingImports System.Runtime.Remoting.Channels.HttpImports System.Runtime.Remoting.ChannelsImports InterFaceRemotingPublic Class RemotingServer Inherits MarshalByRefObjectImplements InterFaceRemoting.InterFaceRemotingPrivate strData As StringPublic Function GetValue() As String ImplementsInterFaceRemoting.InterFaceRemoting.GetValueReturn strDataEnd FunctionSub New()strData = "testing.."End SubPublic Sub SetValue(ByVal value As String) ImplementsInterFaceRemoting.InterFaceRemoting.SetValuestrData = valueEnd SubEnd ClassModule ModuleRemotingStartUpSub Main()Dim objHttpChannel As HttpChannelConsole.WriteLine("Server Started...")objHttpChannel = New HttpChannel(1234)ChannelServices.RegisterChannel(objHttpChannel)RemotingConfiguration.RegisterWellKnownServiceType(GetType(RemotingServer),"RemoteObject", WellKnownObjectMode.Singleton)Console.WriteLine("Server registered and listening waiting for clients...")Console.ReadLine()End SubEnd ModuleIn the code above, Channel object is created and registered. Server then hosts the object so that client can connect to it. This is the time when we specify what mode the server object will be created i.e. Singleton or SingleCall. This is done by the following below given code. Note in sample we are hosting the server object in singleton mode that means that the same object
will be shared between all clients. Also note the server object is implementing "InterFaceRemoting" and inheriting from "MarshalByRefObject".
RemotingConfiguration.RegisterWellKnownServiceType(GetType(RemotingServer),"RemoteObject", WellKnownObjectMode.Singleton) In the last section the client will connect to this hosted remoting object.
Imports SystemImports System.Runtime.RemotingImports System.Runtime.Remoting.Channels.HttpImports System.Runtime.Remoting.ChannelsImports InterFaceRemotingModule ModuleStartClientSub Main()Dim objHttpChannel As New HttpChannelDim objRemoting As InterFaceRemoting.InterFaceRemotingChannelServices.RegisterChannel(objHttpChannel)objRemoting =CType(Activator.GetObject(GetType(InterFaceRemoting.InterFaceRemoting),"http://localhost:1234/RemoteObject"),InterFaceRemoting.InterFaceRemoting)Console.WriteLine("Referenced the main object.... Now displaying Data")Console.WriteLine("Value on server :- " & objRemoting.GetValue.ToString())Console.WriteLine("Press enter to Terminate")Console.ReadLine()End SubEnd Module Finally you need to run server and then client.
No comments:
Post a Comment