Any component inherited from marshalbyrefobject can be remotely accessed! However, TCP pipelines are not the best method for end users who access deployed components. This is because the socket process requires scalability, security, and performance, which makes the components very huge. The HTTP pipeline provides security, password options, and tested scalability and performance. You can also use soap for better interoperability. These components reside in IIS. The HTTP pipeline uses the HTTP protocol. This pipe uses the soap formatting program to encode the communication into XML.
Using system;
Using system. collections;
Namespace bankchat {
Public class chat: system. externalbyrefobject
{
// Declare an array
Static arraylist messages = new arraylist ();
Public chat ()
{
System. Console. writeline ("Welcome: savemymoney! ");
}
Public void addmsg (string custid, string MSG)
{
// Add information to the array
Messages. Add (MSG );
Console. writeline ("from:" + custid + ", message:" + MSG );
}
Public String getallmessages ()
{
String allmessages = NULL;
// Loop
Foreach (string msg1 in messages)
{
Allmessages + = msg1 + "N ";
}
Return allmessages;
}
}
}
Using system;
Using system. runtime;
Using system. runtime. remoting;
Using system. runtime. remoting. channels;
Using system. runtime. remoting. channels. HTTP;
Using bankchat;
Namespace bankserver
{
Public class classserver
{
Public static void main (string [] ARGs)
{
// Generate an HTTP Pipeline
Httpchannel channel = new httpchannel (9932); // register the MPs queue
Channelservices. registerchannel (Channel); // registers an object
Remotingconfiguration. registerwellknownservicetype (typeof (CHAT), "chat", wellknownobjectmode. singlecall );
System. Console. writeline ("hello from bank server ");
System. Console. Readline ();
}
}
}
Using system;
Using system. runtime. remoting;
Using system. runtime. remoting. channels;
Using system. runtime. remoting. channels. HTTP;
Using bankchat;
Namespace Client
{
Public class classclient
{
Public static void main (string [] ARGs)
{
Console. writeline ("Please enter your customer ID ");
String custid = console. Readline ();
Console. writeline ("Please enter your port number ");
String strport = console. Readline ();
Int Port = int32.parse (strport );
Channelservices. registerchannel (New httpchannel (port ));
// Access a remote object
Chat chatclient = (CHAT) activator. GetObject (typeof (bankchat. Chat), "http: // localhost: 9932/chat ");
If (chatclient = NULL)
{
Console. writeline ("unable to get Remote Reference ");
}
Else
{
String MSG;
Do
{
Console. writeline ("Enter your message or type exit? To quit ");
MSG = console. Readline ();
Chatclient. addmsg (custid, MSG );
} While (msg. tolower (). Equals ("exit") = false );
Console. writeline ("the session ends. Thank you for your participation! N ");
String allmessage = chatclient. getallmessages ();
Console. writeline (allmessage );
}
Console. Read ();
}
}
}
Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using bankchat;
Using system. runtime. remoting;
Using system. runtime. remoting. channels;
Using system. runtime. remoting. channels. HTTP;
Using system. Threading;
Namespace chatclient
{
/// <Summary>
/// Summary of form1.
/// </Summary>
Public class form1: system. Windows. Forms. Form
{
Private system. Windows. Forms. Button button1;
Private system. Windows. Forms. textbox textbox1;
Private system. Windows. Forms. textbox textbox2;
Private system. Windows. Forms. textbox textbox3;
Chat chatclient;
Private system. Windows. Forms. textbox textbox4;
Static int port;
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. Container components = NULL;
Public form1 ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();
//
// Todo: add Any constructor code after initializecomponent calls
//
}
/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. button1 = new system. Windows. Forms. Button ();
This. textbox1 = new system. Windows. Forms. Textbox ();
This. textbox2 = new system. Windows. Forms. Textbox ();
This. textbox3 = new system. Windows. Forms. Textbox ();
This. textbox4 = new system. Windows. Forms. Textbox ();
This. suspendlayout ();
//
// Button1
//
This. button1.location = new system. Drawing. Point (416,232 );
This. button1.name = "button1 ";
This. button1.tabindex = 0;
This. button1.text = "button1 ";
This. button1.click + = new system. eventhandler (this. button#click );
//
// Textbox1
//
This. textbox1.location = new system. Drawing. Point (96,232 );
This. textbox1.name = "textbox1 ";
This. textbox1.size = new system. Drawing. Size (256, 21 );
This. textbox1.tabindex = 1;
This. textbox1.text = "textbox1 ";
//
// Textbox2
//
This. textbox2.location = new system. Drawing. Point (96, 24 );
This. textbox2.multiline = true;
This. textbox2.name = "textbox2 ";
This. textbox2.size = new system. Drawing. Size (256,120 );
This. textbox2.tabindex = 2;
This. textbox2.text = "textbox2 ";
//
// Textbox3
//
This. textbox3.location = new system. Drawing. Point (424, 48 );
This. textbox3.name = "textbox3 ";
This. textbox3.tabindex = 3;
This. textbox3.text = "textbox3 ";
//
// Textbox4
//
This. textbox4.location = new system. Drawing. Point (424,104 );
This. textbox4.name = "textbox4 ";
This. textbox4.tabindex = 4;
This. textbox4.text = "textbox4 ";
//
// Form1
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (640,349 );
This. Controls. Add (this. textbox4 );
This. Controls. Add (this. textbox3 );
This. Controls. Add (this. textbox2 );
This. Controls. Add (this. textbox1 );
This. Controls. Add (this. button1 );
This. Name = "form1 ";
This. Text = "form1 ";
This. Load + = new system. eventhandler (this. form#load );
This. resumelayout (false );
}
# Endregion
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}
Private void form1_load (Object sender, system. eventargs E)
{
// Random r = new random (1000 );
// Int Port = R. Next (10000 );
// MessageBox. Show (convert. tostring (port ));
// Access a remote object
Channelservices. registerchannel (New httpchannel (port ));
Chatclient = (CHAT) activator. GetObject (typeof (bankchat. Chat), "http: // localhost: 9932/chat ");
Thread OBJ = new thread (New threadstart (getmessage ));
OBJ. Start ();
}
Public void getmessage ()
{
Chat S = new chat ();
String allmessage;
Int length = 0;
Int L = 0;
While (true)
{
Allmessage = S. getallmessages ();
While (length> = L)
{
// Allmessage = chatclient. getallmessages ();
If (allmessage! = NULL)
{
Length = allmessage. length;
L = allmessage. length;
Textbox2.appendtext (allmessage );
}
L ++;
// Allmessage = chatclient. getallmessages ();
}
}
}
Private void button#click (Object sender, system. eventargs E)
{
Chatclient. addmsg (textbox3.text, textbox1.text );
}
}
}