Romoting communication demo (sorting)

Source: Internet
Author: User

1. Server

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. runtime. remoting;
Using system. runtime. remoting. channels;
Using system. runtime. remoting. channels. TCP;
Using system. runtime. remoting. channels. HTTP;
Using generalservice;

Namespace remotingserver
{
Public class Server
{
Public static int main (string [] ARGs)
{
// Channel implementation for message transmission over TCP
Tcpchannel chan1 = new tcpchannel (8085 );
// Implement the client channel for transmitting messages using HTTP for remote calls
Httpchannel chan2 = new httpchannel (8086 );
// Provides static methods to help you remotely process channel registration, resolution, and URL discovery. Unable to inherit this class
Channelservices. registerchannel (chan1, false );
Channelservices. registerchannel (chan2, false );
// Provides various static methods for configuring remote Structures
Remotingconfiguration. registerwellknownservicetype
(
Typeof (helloserver ),
"Sayhello ",
Wellknownobjectmode. Singleton
);

System. Console. writeline ("Press ENTER key to exit ");
System. Console. Readline ();
Return 0;
}
}
}

 

2. Service

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace generalservice
{
Public class helloserver: marshalbyrefobject
{
Public helloserver ()
{
Console. writeline ("helloserver activated ");
}

Public String hellomethod (string name)
{
Console. writeline (
"Server hello. hellomethod: {0}", name );
Return "Hi There" + name;
}
}

}

 

3. Client

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. IO;
Using system. runtime. remoting;
Using system. runtime. remoting. channels;
Using system. runtime. remoting. channels. TCP;
Using system. runtime. remoting. channels. HTTP;
Using generalservice;

Namespace remotingclient
{
Public class client
{
Public static void main (string [] ARGs)
{
// Obtain the remote object through the TCP Channel
Tcpchannel chan1 = new tcpchannel ();
Channelservices. registerchannel (chan1, false );
// Activator contains specific methods for creating object types locally or remotely, or obtaining references to existing remote objects. Unable to inherit this class
Helloserver obj1 = (helloserver) activator. GetObject (
Typeof (generalservice. helloserver ),
"TCP: // localhost: 8085/sayhello ");
If (obj1 = NULL)
{
System. Console. writeline (
"Cocould not locate TCP server ");
}
// Obtain the remote object through the HTTP Channel
Httpchannel chan2 = new httpchannel ();
Channelservices. registerchannel (chan2, false );
Helloserver obj2 = (helloserver) activator. GetObject (
Typeof (generalservice. helloserver ),
"Http: // localhost: 8086/sayhello ");
If (obj2 = NULL)
{
System. Console. writeline (
"Cocould not locate HTTP server ");
}

Console. writeline (
"Client1 TCP hellomethod {0 }",
Obj1.hellomethod ("caveman1 "));
Console. writeline (
"Client2 HTTP hellomethod {0 }",
Obj2.hellomethod ("caveman2 "));
Console. Readline ();
}
}
}

 

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.