Remo Ting

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Peers Technologies Pvt Ltd

Remoting

Remoting
Distributed Technology:

It is a concept if maintaining the application in different system.

Advantages:

Reusability

Easy maintenance

Application Development: Application development generally contains the following layers


1.

User Interface Layer


Ex: ASPX files

2.

Business Logic Layer


Ex: ASPX.CS or .CS files

3.

Data Store Layer


Ex: SQL Server

Application Architecture:
1.

Single Tire

(if 3 layer r present in one system)

2.

Two Tire (if 3 layer r present in two systems)

3.

Three Tire (if 3 layer r present in three system)

4.

n Tire (if 3 layer r present in n system)

1.

Single Tire:

2.

Two Tire:

1-Tier Architecture is the simplest, single tier on single user, and is the
equivalent of running an application on a personal computer. All the required component
to run the application are located within it. User interface, business logic, and data
storage are all located on the same machine. They are the easiest to design, but the least
scalable. Because they are not part of a network, they are useless for designing web
applications.

2-Tier Architectures supply a basic network between a client and a server. For
example, the basic web model is a 2-Tier Architecture. A web browser makes a request
from a web server, which then processes the request and returns the desired response, in
this case, web pages. This approach improves scalability and divides the user interface
from the data layers. However, it does not divide application layers so they can be utilized

Faculty: Suresh Naidu

Page 1

Peers Technologies Pvt Ltd

Remoting

separately. This makes them difficult to update and not specialized. The entire application
must be updated because layers arent separated.

3.

Three Tire:

3-Tier Architecture is most commonly used to build web applications. In this

model, the browser acts like a client, middleware or an application server contains the
business logic, and database servers handle data functions. This approach separates
business logic from display and data. So the 3 layers commonly known as: Presentation
Layer (PL/UI), Business Logic Layer(BLL) & Data Access Layer(DAL).

4.

N-Tire: N-tier application structure implies the client/server program model. Where there are more
than three distribution levels or tiers involved, the additional tiers in the application are usually
associated with the business logic tier.

Two tire & three tire comes into the distributed technology. We can develop distributed applications with the
following technologies.

DCOM (Microsoft)

RMI(Sun Micro System)

Faculty: Suresh Naidu

Page 2

Peers Technologies Pvt Ltd

Remoting

DCOM

Supports only in windows platform

Communication between only MS products

Remoting: Remoting provides .NET to .NET communication in homogeneous and heterogeneous platforms.
Channel: Chanel is a communication between client and server.

Remoting supports the following types of channels.


1.

TCP Channel

2.

HTTP Channel

1.

TCP Channel: It supports,

2.

a.

Homogeneous platforms

b.

Network based stream

c.

Platform dependent

HTTP Channel: It supports,


a.

Heterogeneous platforms

b.

XML based stream

c.

Platform independent

Serialization: Process of converting an object into the stream


De-serialization: Process of constructing an object from the stream

Faculty: Suresh Naidu

Page 3

Peers Technologies Pvt Ltd

Remoting

Formatters: Microsoft provides the following formatters to perform serialization and de-serialization.
1.

Binary Formatter

2.

SOAP formatter

1.

Binary Formatter: It will provide network based stream and used by TCP channel.

2.

SOAP Formatter: It will provide XML based stream and used by HTTP channel.

Remoting Activation Modes: The following hierarchy represent the activation modes.

Marshalling: It is the concept of producing stream to be transmitted between one process to another process.
MarshallByRef: Object will be maintained by the server application and client application will be provided with
reference of the objects.
Server Activated: When client makes a method call, the object will be created on the server this is called as
Server Activated.
Singlecall

One object on the server will provide service to only one client
It does not provide any state

Faculty: Suresh Naidu

Page 4

Peers Technologies Pvt Ltd

Remoting

Singleton

One object on the server will provide services to different client


It provides state global to all users

Creating Channel: If you want to create a channel, we have to use TCPChannel class.
TCPChannel Class: This class is defined in System.Runtime.Remoting.Channels.Tcp namespace and it
provides a channel implementation that uses the TCP protocol to transmit messages.
TCPChanne(int): Initializes a new instance (constructor) of the TcpChannel class with a server channel that
listens on the specified port.
Ex:
TCPChannel obj = new TCPChannel(2001);

ChanelServices Class: This class is defined in System.Runtime.Remoting.Channels. It provides static


methods to aid with remoting channel registration, resolution, and URL discovery. This class cannot be
inherited.
RegisterChannel(IChannel): Registers a channel with the channel services. This method receives
TCPChanne class object
Ex:
ChanelServices.RegisterChannel(obj);

RemotingConfiguration Class: This class is defined in System.Runtime.Remoting. Provides various static


methods for configuring the remoting infrastructure.
RegisterWellKnownServiceType(Type, String, WellKnownObjectMode): Registers an object Type on the
service end as a well-known type, using the given parameters to initialize a new instance of
WellKnownServiceTypeEntry. WellKnownObjectMode means Singlecall or Singleton.

Ex:
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Demo), "MyName",
WellKnownObjectMode.SingleCall);
Parameters:
1. well- known type i.e. here Demo is a class
2. Alias name and its user defined. It is useful to refer the class for client and server communication
3. How you want to maintain object.

Faculty: Suresh Naidu

Page 5

Peers Technologies Pvt Ltd

Remoting

Remoting Sample Application (Singleton/Singleton):


Create the following application:
1.

Business Logic Application


Create Class Library Project (RemoteClass)

2.

Server Application
Create Console Application (RemoteServer)

3.

Client Application
Create Windows Application (RemoteClient)

Communication between one application to another represents as follows:

(Class Library)

RemoteClass

Get reference of RemoteClass.dll

Get reference of RemoteClass.dll


response
(System.Runtime.Remoting.dll)

(System.Runtime.Remoting.dll)

RemoteServer

request

(Console Application)

RemoteClient
(Windows Application)

Both Server (console application) & Client (windows application) applications are using the class (Demo)
defined in business application (class library).
Note: You have to get reference of System.Runtime.Remoting in both client and server applications, in
order to use the following classes in our applications.

TCPChannel Class Class

ChanelServices Class

RemotingConfiguration Class

I.

Remoting Sample Application for Singleton

1.

Business Logic Application


a.

Create C#.Net New Class Library project and save as RemoteClass and write the following
statements in class1.cs

Faculty: Suresh Naidu

Page 6

Peers Technologies Pvt Ltd

Remoting

public class Demo : MarshalByRefObject


{
int cnt;
public int Increment()
{
cnt += 1;
return cnt;
}
}

Note: Above class should be inherit from MarshalByRefObject class, which is defined in System namespace.
b.

2.

Build an application and you can find RemoteClass.dll in ~/bin/debug within project location
folder.

Server Application
a.

Create C#.Net New Console Application project and save as RemoteServer.

b.

In order to access remoting classes in our applications, first get the reference for
System.Runtime.Remoting.dll as shown below.

c.

In solution explorer, right click project name, click on Add Reference and select
System.Runtime.Remoting.dll from .NET tab as shown below.

d.

Write the following statements in Program.cs

using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using RemoteClass;
namespace RemoteServer
{
class Program
{
static void Main(string[] args)

Faculty: Suresh Naidu

Page 7

Peers Technologies Pvt Ltd

Remoting

{
TcpChannel c = new TcpChannel(2001);
ChannelServices.RegisterChannel(c);
//Will Create only one object to the Demo class and maintaining that object.
//This object will share to each and every client, because we are using Singleton
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Demo), "MyName", WellKnownObjectMode.Singleton);
Console.WriteLine("Server Started");

Console.WriteLine("Press enter to stop");


Console.Read();

}
e.

Execute an application (DO NOT CLOSE WINDOW)

Note:
In the above program:
2001: Port Number, User defined number
Demo: class name, which is defined in class library (above) project
MyName: Alias name, which we have to refer same in client (below) application
Singleton: How you want to maintain object?
3.

Client Application
a.

Create C#.Net New Windows Application project and save as RemoteClient.

b.

In order to access remoting classes in our applications, first get the reference for
System.Runtime.Remoting.dll as shown below.

c.

In solution explorer, right click project name, click on Add Reference and select
System.Runtime.Remoting.dll from .NET tab as shown below.

f.

Design Form1 as follows.

Faculty: Suresh Naidu

Page 8

Peers Technologies Pvt Ltd

g.

Remoting

Write the following statements in Form1.cs

using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using RemoteClass;
private void Button1_Click(object sender, EventArgs e)
{
try
{
Demo obj = (Demo)Activator.GetObject(typeof(Demo), "tcp://localhost:2001/MyName");
MessageBox.Show(obj.Increment().ToString());
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}

h.

Execute an application

Note:
In the above program:
Demo: class name, which is defined in class library (above) project
MyName: Alias name, which we have used in server in (above) application
tcp: using tcp channel
localhost: Server application existing in local system

Faculty: Suresh Naidu

Page 9

Peers Technologies Pvt Ltd

Remoting

Output: How to execute above application?


Step 1: Execute Windows (Client)
Application.

Step 4: Goto ~bin/debug folder in


our windows application

Step 7: Again double click on


.exe (RemoteClient) file

Step 2: Click on Button

Step 5: Double click on .exe


(RemoteClient) file

Step8: Will display 3

Step 3: Will display 1

Step6: Will display 2

If we consider, each window is one client. Totally three clients are connected to the server. Whenever client
gives a request to the server, its created only one object and that object is sharing to each and every client.
i.e. server is maintain or preserving the value. For that reason, we are receiving latest value always, when we
click on button/

II. Remoting Sample Application for Singlecall


a.

Update the following statement in server (console) application.

//Will Create object to the Demo class and maintaining that object.
//Here one object will create for one client, because we are using SingleCall
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Demo), "MyName", WellKnownObjectMode.SingleCall);
b.

Execute an server (console) application (DO NOT CLOSE WINDOW)

c.

Execute windows (server) application

Faculty: Suresh Naidu

Page 10

Peers Technologies Pvt Ltd

Remoting

Output: How to execute above changes?

Step 1: Execute Windows (Client)


Application.

Step 4: Goto ~bin/debug folder


in our windows application

Step 7: Again double click on .exe


(RemoteClient) file

Step 2: Click on Button

Step 5: Double click on .exe


(RemoteClient) file

Step8: Will display 1

Step 3: Will display 1

Step6: Will display 1

If we consider, each window is one client. Totally three clients are connected to the server. Whenever client
gives a request to the server, its created one object to one clineti.e. server is maintain or preserving all
objects information. For that reason, we are receiving same value always, when we click on button.

***

Faculty: Suresh Naidu

***

***

Page 11

You might also like