How to implement a simple remoteing instance

Source: Internet
Author: User

Let's take 20 minutes to create a simple remoteing example.
First, we create a remote object named remoteing in DLL. This object is a bit similar to the interface file function in EJB, but it is not completely responsible for communication. We will also write the business logic in this interface:
Create a class library project remoteobject:
Using system;

Namespace remoteobject
{
Public class myobject: marshalbyrefobject
{
Public int add (int A, int B)
{
Return A + B;
}
Public String STR ()
{
Return "I am come from server :";
}
}
}
Compile it.
Then create the server-side program remoteserver:
Using system;
Using system. runtime. remoting;

Namespace remoteserver
{
Class myserver
{
[Stathread]
Static void main (string [] ARGs)
{
Remotingconfiguration. Configure ("remoteserver.exe. config ");
Console. Readline ();
}
}
}
And add a configuration file app. config:
<Configuration>
<System. runtime. remoting>
<Application name = "remoteserver">
<Service>
<Wellknown type = "remoteobject. myobject, remoteobject" objecturi = "remoteobject. myobject"
Mode = "Singleton"/>
</Service>
<Channels>
<Channel ref = "TCP" Port = "9999"/>
</Channels>
</Application>
</System. runtime. remoting>
</Configuration>
Test the compiled DLL file to the bin directory of the new server program, add references, and compile the server program.
Create the client remoteclient:
Using system;

Namespace remoteclient
{
Class myclient
{
[Stathread]
Static void main (string [] ARGs)
{
Remoteobject. myobject remoteclient = (remoteobject. myobject) activator. GetObject (typeof (remoteobject. myobject), system. configuration. configurationsettings. deleettings ["serviceurl"]);
Console. writeline (remoteclient. STR () + remoteclient. Add (1, 2 ));
Console. Readline ();
}
}
}
Create the configuration file app. config of the client program:
<Configuration>
<Deleetask>
<Add key = "serviceurl" value = "TCP: // localhost: 9999/remoteobject. myobject"/>
</Appsettings>
</Configuration>
Test the compiled DLL file to the bin directory of the new client program, add references, and compile the server program.
Run the server program first, and then run the client program. The following page is displayed:


Now we have completed a simple remoteing instance, which implements remote communication with EJB, But now let's look at how different he and EJB are:
1. The remoteing transaction is written in DLL (Interface), and The EJB is written on the server.
2. A server program is equivalent to a container in EJB. It can load n interfaces for business implementation. By setting the channel type, port number, and interface name in the configuration file, determine which DLL the client program accesses.
3. you can also see the example of customer segment activation and server activation, although the completed business is written in the DLL, but in fact, the applied space and the memory used by the business are in the server. The DLL object uses its own constructor to construct some memory addresses on the server, and space for storing variables or other functions, so the DLL business operations are not on the client.
Therefore, we can also know that the memory address for implementing the transaction is actually on the server.
(Thanks to lovecherry's blog. I have added some understanding and thinking in my article. If you want to see the pure version of remoting, please refer to http://www.cnblogs.com/lovecherry. you are not sure to take advantage of others .)

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.