For these types of questions interviewer expects small and sweet answers. He is basically looking at what you know about the specific subject. For these type of question this post will provide detail code which is not necessary to be said during interview. Only the basic steps and overall brief are enough to convince that you have knowledge about the subject. Even though this question has detail code and answer say only what is needed in interview.
Remoting has at least three sections :-
- Common Interface which will be shared between them.
- Server.
- Client.
Solution Explorer of Remoting Project
Above is the figure which shows the three important project sections needed to implement remoting.
First important section is the common interface between Server and Client.”InterFaceRemoting” project has the interface code. For sample project interface is very simple with only two methods :- SetValue and GetValue.
Public Interface InterFaceRemoting
Sub SetValue(ByVal value As String)
Function GetValue() As String
End Interface
Second important section is the server.In this sample server is using HTTP channel and the server object is singleton.
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Channels
Imports InterFaceRemoting
Public Class RemotingServer
Inherits MarshalByRefObject
Implements InterFaceRemoting.InterFaceRemoting
Private strData As String
Public Function GetValue() As String Implements
InterFaceRemoting.InterFaceRemoting.GetValue
Return strData
End Function
Sub New()
strData = “testing..”
End Sub
Public Sub SetValue(ByVal value As String) Implements
InterFaceRemoting.InterFaceRemoting.SetValue
strData = value
End Sub
End Class
Module ModuleRemotingStartUp
Sub Main()
Dim objHttpChannel As HttpChannel
Console.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 Sub
End Module
Following is detail explanation :-
- Channel object is created and registered.
Following is the code.
Dim objHttpChannel As HttpChannel
Console.WriteLine(“Server Started....”)
objHttpChannel = New HttpChannel(1234)
ChannelServices.RegisterChannel(objHttpChannel)
- 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)
Now comes the final section that is third section the client which will connect to this hosted remoting object.
Following is a detail explanation of client code :-
- First we create the channel i.e. HTTP. Note whatever channel the server is using same will be used by the client.
ChannelServices.RegisterChannel(objHttpChannel)
- As said before the common interface i.e.“InterFaceRemoting” will be used to communicate with client.
- After that we can get the server object reference using following code
objRemoting = CType(Activator.GetObject(GetType(InterFaceRemoting.InterFaceRemoting),
“http://localhost:1234/RemoteObject”), InterFaceRemoting.InterFaceRemoting)
Then the client can make method call as if the object is local. But actually the object is a proxy.
Console.WriteLine(“Value on server :- “ & objRemoting.GetValue.ToString())
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Channels
Imports InterFaceRemoting
Module ModuleStartClient
Sub Main()
Dim objHttpChannel As New HttpChannel
Dim objRemoting As InterFaceRemoting.InterFaceRemoting ChannelServices.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 Sub
End Module
You an run the program and see the output. For running the program run the server program which is in server directory. Run “Server.exe” from BIN directory. If the EXE runs properly following will be the screen as shown below.
Running Server Program of Remoting
Now run “Client.exe” from client folder in BIN directory.Following will be the output seen.This means that the client connected to the server program and displayed the data in the server object. In the server object we have initialized value “testing......”. In constructor of class “RemotingServer” same value is displayed at the client side as shown in figure below.
Client Program output of Remoting
What are the situations you will use singleton architecture in remoting ?
If all remoting clients have to share the same data singleton architecture will be used.
No comments:
Post a Comment