Blog

Working with distributed programming in C# .net

16 October, 2020

Related Blogs

Screenshot_41  

  • Distributed programming is a concept of sharing application logic over the network A collection of computers, which are connected together is called as a network
  • A collection of computers, which are connected together is called a network
  • To establish a network, a protocol is required protocol provides a set of rules, which need to be allowed while transferring the data over the network Generally network can be established with the help of TCP or HTTP Protocol.

  Generally distributed programming is required to develop3- Tier or N- Tier architectures. 

These core tutorials will help you to learn the fundamentals of .NET. For an in-depth understanding and practical experience, explore Online ".NET Training".

Screenshot_42    

Working with .NET Remoting

  1. .NET Remoting helps to develop distributed programming architecture.
  2. Distributed programming allows to share the logic over the network.
  3. Distributed programming allows to develop secured applications.

  Screenshot_43  

  1. The connection between two computers is called a “channel”.
  2. Remoting supports HTTP Channel and Tep channel
  3. The communication in between two CLR’s is also called as .NET remoting
  4. To start the communication a PORT need to be reserved.
  5. The starting point of a communication channel is called as PORT.
  6. Os supports o to 65535 ports, from which some ports are PRE-RESERVED.

Ex:-

HTTP---80 SMTP---25 FTP-----31 Web logic Servers-----7070,8080,9090

  1. .NET Remoting Architecture

    Screenshot_44    

  1. Converting object stream into byte stream is called as “Marshalling” and vice versa is called as “De- Marshalling”.
  2. Marshaling will be done with the help of STUB and De-Marshalling by the skeleton.
  3. To work with .NET remoting, micro soft introduced system Run time. Remoting Assembly
  4. Marshal By Ref object is a predefined class, which helps in Marshalling and De-Marshalling

In order share the Logic, Some Reouree files need to be copied from the server machine into the client machine.    

Steps for Developing Remoting Application:-

Step1:- Write an interface with a set of abstract methods and compile into a DLL file.  

Step2:- Write a server program by overriding Interface methods and Reserve a channel over a PORT.  

Step3:- Write a client program to create a connection with server and call interface methods.  

Step4:- Start server then start client programs  

Obs:-

The interface DLL must be copied into both server and client machines.  

Developing 3- Tier Architecture with Remoting:-

  Screenshot_45  

  Screenshot_46

Screenshot_24Open oracle software and create a table with name users and with two columns used and pwd

Screenshot_24Create table users(Usid varchor2(20), Pwd varchar 2(10));

Screenshot_24Add two records as follows Insert into users values(‘teja’, ’abc’); Insert into users values(‘Ravu’, ’xyz’);

Screenshot_24Save or commit;  

  1. Developing Interface with a let of abstract methods

Screenshot_24Open class Library project with the project name Middle Interface. Namespace middle Interface { Public interface MI { Bool check user(String a, String b); }//HI }Middle Inter face

Screenshot_24Build the project(build menu

Screenshot_24 Build solution)

Obs:- Middle interface.dll is created under D:/C194/ Middle interfaceBinDebug folder  

  1. Developing server Application

Screenshot_24Open console Application project with project name Middle server

Screenshot_24 project menuàadd referenceà System. Runtime. Remoting

Screenshot_24Project menuàAdd referenceàbrowseà Middle Interface.DLL

Screenshot_24Using system. Run time. Remoting;

Screenshot_24 Using system. Run time. Remoting. channels;

Screenshot_24Using system. Run time. Remoting channels. TCP;

Screenshot_24Using system. Data. oleDb;

Screenshot_24

Code in GD(Before Main)

{

Class M Server: Marsha L By Ret object, HI

{

Public bool check user(String a, string b)

{

String q=” select* from users where Usid=’”+a+”’ and pwd=’”+”’;

oleDb connection cn = new oleDb connection (“user id = scott; password= tiger;provider=msdaora.1”) Cn.open();

oleDb command cmd= new oleDb command(q.cn); object obj=cmd. Execute scalor();

H(OBJ==NULL) Return false; Else Return true;

}

//check user method }//m server-class àcode for

main()

{

TCP channel t= new TCP channel(1600);

Channel Services. Register channel(t);

Remoting configuration. Register well known service type (type of (m server),”abc”, well known object mode Single call);

Console. write line(“server is ready to use”); Console. Read key();

}

Screenshot_24Execute the project Then a TCP Server will be created with the following URL Tcp://Local host:1600/abc

  1. Developing a client application

Screenshot_24open a new .NET Window(startàrunàdevenr)

Screenshot_24open WFAP with project name Middle client

Screenshot_24Project menuà Add ReferenceàBrowseà Middle Interface.DLL

Screenshot_24Design the form as shown

Screenshot_24Using middle Interface;

Screenshot_24

code for button1-click

{

MI X=(MI)Activator. Get object(type of(MI)”tcp://Local host:1600/abc”);

BooL B= X. Check user (text box1.text,Text box2. text);

If(b==true) Message box. show(“valid user”);

Else Message box. show(“not a valid user”);

 }

Screenshot_47    

Obs:- Remoting configuration. Register well-known service type() takes3 arguments  

Org 1) Type of the class, where interface methods are over rided .  

Org 2) Provide an alias name to the server  

Org3) Specifies the mode of the communication   Remoting supports two types of communication model with the help of well-known object mode enum   Screenshot_48    

Screenshot_24Single call is tastes in communication when compared with single ton

Screenshot_24In single call, a separate object will be created for every client

Screenshot_24In single ton, only one object will be created for all the clients When number of clients are limited then use single all other wise use single ton Activator class is used to open a connection with my specified URL 2 Developing an application like yahoo messenger  with off line support and two consecutive messages quid not be same  

Screenshot_49  

Screenshot_24Open class library project with project name yahoo Interface

{

Public Interface YI

{

String Get TEXT();

}

}

Screenshot_24Build the project(Build menuScreenshot_24Build solution)  

Obs:-

Yahoo Interface.dll is created under D:C194/Yahoo Interface/Bin/Debug folder

Screenshot_24Open windows farms Application project with project name yahoo server

Screenshot_24Design the form as follows:-

Screenshot_24Project

Screenshot_24Add Reference

Screenshot_24 System. Run time. Remoting

Screenshot_24project menu

Screenshot_24 Add Reference

Screenshot_24Browe

Screenshot_24 Yahoo Interface.DLL

Using system. Runtime. Remoting;

Using system. Runtime. Remoting. channels;

Using system. Runtime. Remoting. Channels.TCP;

Using Yahoo Interface;

Screenshot_24Code in GD

Static string S;

Class y server: Marshal By Ret Object.YI

{

Public string Get text()

{

Return Farm1.s;

}//Get Text

}//4 Server

Screenshot_24

Code for Button1-click(send)

{

Forml. s=text box2.text;

}

Code for farm1-load Event

{

TCP Channel=new TCP Channel(4829);

Channel Services. Register channel(t);

Remoting configuration. Register well known Service type(type of (4 Server),”yahoo”, well known object mode. single ton);

Text box1.text=”server is Ready”;

}

Screenshot_24Execute the project  

OBS:-

Then a tcp server will be created with the following URL Tcp://Local host:4829/yahoo

Screenshot_24Developing client application

Screenshot_24Open a new .NET Window

Screenshot_24Open WFAP with project name yahoo client

Screenshot_24Place two text boxes, one button, and a timer control with enabled = false

Screenshot_24Project menu

Screenshot_24Add referenceScreenshot_24BrowseScreenshot_24Yahoo Interface.dll

Screenshot_24Using yahoo Interface; Screenshot_24code in GD String s1,s2; YI X;

Screenshot_24code for the formal – Load event

{

X=(YI)Activator. Get Object(type of (yI) “tcp://Local host:4829/yahoo”);

Text box1.text=x. get text();

Timer1.Enabled=true;

}

àcode for time:-1-tick event

{

S1-X.Get Text();

If(s1 !=s2)

Text box1.text=text box1.text+ Environment.  new line+s1;

S2=s1;

}

F5  

Security:-

  Screenshot_50

  1. Security is a concept of restricting Anonymous(unknown)accessibility.
  2. C#.NET Supports security in two ways:-
  3. Crypto Graphy
  4. Role Based Security