A code example of remoting (using remoting to send information)

Source: Internet
Author: User

(1). Description
A Remote Call example.
This example shows how to send a message by calling a remote method (custom information can be displayed in the remote method.
The implementation principle is like this: the client cannot directly call a remote object. It must first request the server host program through a channel. When receiving a client request,
The. NET remote processing framework generates the required remote object in the application domain of the host component, and executes the methods in the remote object.
(2) Implementation Scheme
Before that, we will introduce several types:
1. serializable classes: The property is marked and can be transferred between processes, applications, and computers.
2. classes that can be called remotely: directly or indirectly inherit the system. externalbyrefobject class and can be remotely activated.
3. General class: distributed structures cannot be built for local calls.
1. Create three projects first:
Remoteobject: provides remote objects for the client to call.
Simpleclient: used to send a request to a server program and call a remote object (winform)
Simpleserver: listens to client requests and creates an object (winform)
2. Create a remote call class under the remoteobject project: remoteobject. CS
Create the form1.csand simpleclient.exe. config configuration files under the simpleclient project.
The configuration file specifies the server address, channel, and other information. The following code provides a detailed description.
Create the form1.csand simpleserver.exe. config configuration files under the simpleserver project.
The configuration file is used to specify the address and channel of the client that receives the request. The following code provides a detailed description.
(3 ).
Source code of each file:
1. remoteobject. CS
Using system;
Using system. Windows. forms;
Namespace remoteobjects
{
Public class remoteobject: system. externalbyrefobject // inherit this class to be remotely activated and called
{
Public remoteobject ()
{
}
  
// Call a method remotely. function: pop up a custom message. Parameter: STR is transmitted from the client.
Public static void method (string Str)
{
MessageBox. Show (STR );
}
}
}
2. form1.cs in the simpleclient project of the client project:
Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using remoteobjects;
Namespace simpleclient
{
Public class form1: system. Windows. Forms. Form
{
Private system. Windows. Forms. textbox textbox1;
Private system. Windows. Forms. Label label1;
Private system. Windows. Forms. Button button1;
Private system. componentmodel. Container components = NULL;
Public form1 ()
{
Initializecomponent ();
}
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Region code generated by Windows Form Designer
///

/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
///

Private void initializecomponent ()
{
This. textbox1 = new system. Windows. Forms. Textbox ();
This. label1 = new system. Windows. Forms. Label ();
This. button1 = new system. Windows. Forms. Button ();
This. suspendlayout ();
//
// Textbox1
//
This. textbox1.location = new system. Drawing. Point (40, 88 );
This. textbox1.multiline = true;
This. textbox1.name = "textbox1 ";
This. textbox1.size = new system. Drawing. Size (336,176 );
This. textbox1.tabindex = 0;
This. textbox1.text = "";
//
// Label1
//
This. label1.location = new system. Drawing. Point (40, 32 );
This. label1.name = "label1 ";
This. label1.tabindex = 1;
This. label1.text = "Enter the following information :";
//
// Button1
//
This. button1.location = new system. Drawing. Point (296, 32 );
This. button1.name = "button1 ";
This. button1.tabindex = 2;
This. button1.text = "send ";
This. button1.click + = new system. eventhandler (this. button#click );
//
// Form1
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (416,302 );
This. Controls. Add (this. button1 );
This. Controls. Add (this. label1 );
This. Controls. Add (this. textbox1 );
This. Name = "form1 ";
This. Text = "Send message ";
This. Load + = new system. eventhandler (this. form#load );
This. resumelayout (false );
}
# Endregion
  
[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}
  
Private void form1_load (Object sender, system. eventargs E)
{
// Load the channel to listen for client requests. The configuration file records the client address and other information.
System. runtime. remoting. remotingconfiguration. Configure ("simpleclient.exe. config ");
}
Private void button#click (Object sender, system. eventargs E)
{
// Start to call a remote object (send information)
Remoteobjects. remoteobject. Method (this. textbox1.text );
}
}
}
3.simpleclient.exe. config in the simpleclientproject of the customer end project:
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
4. form1.cs in the simpleserver project of the server project:
Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. runtime. remoting;
Namespace simpleserver
{
///

/// Summary of form1.
///

Public class form1: system. Windows. Forms. Form
{
///

/// Required designer variables.
///

Private system. componentmodel. Container components = NULL;
Public form1 ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();
//
// Todo: add Any constructor code after initializecomponent calls
//
}
///

/// Clear all resources in use.
///

Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Region code generated by Windows Form Designer
///

/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
///

Private void initializecomponent ()
{
//
// Form1
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (292,266 );
This. Name = "form1 ";
This. Text = "form1 ";
This. Load + = new system. eventhandler (this. form#load );
}
# Endregion
///

/// Main entry point of the application.
///

[Stathread]
Static void main ()
{
// Application. Run (New form1 ());
Run ();
}
Private Static void run ()
{
System. runtime. remoting. remotingconfiguration. Configure ("simpleserver.exe. config ");
}
Private void form1_load (Object sender, system. eventargs E)
{
  
}
}
}
5.simpleserver.exe. config in the simpleserverproject of the service end project:
  
  
  
  
  
  
  
  
  
  
  
  
  
  
(4). Notes
1. because the configuration file is added to the root directory by default, You need to copy two configuration files to the root directory/bin/debug so that it and the execution file are in the same directory.
2. Right-click the remoteobject project and select "general". The input type is "class library". It is an application by default. It is used as a class library.
After setting, press Ctrl + Shift + B to generate the class library DLL.
3. Add reference in the simpleserver project and simpleclient respectively: add the DLL: remoteobjects. dll generated by the remoteobject project to the project.
Method: Right-click "Reference"-> "to add the reference"-> "Browse", and find the generated remoteobjects. DLL to add it separately.
4. Right-click the solution and choose "properties"> "select multiple startup project tickets"> "select simpleserver and simpleclient to start at the same time ".
Because: when the server host is running, the client can correctly call the remote object.
5. Press F5 to run. Enter the information and click "send" to call the remote object.
(5). Expansion
You can modify the client configuration file: The localhost in is the name of another machine.
The host program is running on the machine. When called, the message is displayed on the server to send information.
The above code has been tested. If it is incorrect, I hope to criticize and correct it!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.