Head1 Head2

Dotnet Framework

Interoperability

Threading

Remoting

Webservices

Caching Concepts

OOPS

ASP.NET

NET Architecture

ADO.NET

SQL SERVER

UML

Project Management

Remoting Sample

Remoting has atleast three sections :-
√ Common Interface which will be shared between them.
√ Server.
√ Client.

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 64 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.

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.

Top

  Home   |   Submit a Question   |   Share your Interview Experience   |   Contact Us  
Footer
 
Copyright © Gurmeet Singh Khalsa
Website designed by: Freelance Web