Remoting application instance + code, remoting application instance
The following describes a small Remoting instance. The main function is to write client data to the server.
Analysis diagram:
The program code is two console applications (one client and one server) and one class library, as shown below.
Client code:
Using RemotingObjects; using System. collections. generic; using System. linq; using System. runtime. remoting. channels; using System. runtime. remoting. channels. tcp; using System. text; namespace RemotingClient {class Program {static void Main (string [] args) {TcpChannel channel = new TcpChannel (); ChannelServices. registerChannel (channel, false); WriteFileToLocal wfobj = (WriteFileToLocal) Activator. getObject (typeof (RemotingObjects. process), "tcp: // localhost: 8085/RemotingWriteFileToLocalService"); if (wfobj = null) {Console. writeLine ("Couldn't create Remoting Object 'writefiletolocal '. ");} else {Console. writeLine ("Please enter content:"); String name = Console. readLine (); try {wfobj. write (name);} catch (System. net. sockets. socketException e) {Console. writeLine (e. toString () ;}} Console. read ();}}}RemotingClient
Server code:
Using System; using System. collections. generic; using System. linq; using System. runtime. remoting; using System. runtime. remoting. channels; using System. runtime. remoting. channels. tcp; using System. text; namespace RemotingServer {class Program {static void Main (string [] args) {TcpChannel channel = new TcpChannel (8085); ChannelServices. registerChannel (channel, false); RemotingConfiguration. registerWellKnownServiceType (typeof (RemotingObjects. process), "RemotingWriteFileToLocalService", WellKnownObjectMode. singleCall); Console. writeLine ("Server: Press Enter key to exit"); Console. readLine ();}}}RemotingServer
Class library code:
Using System; using System. collections. generic; using System. IO; using System. linq; using System. text; namespace RemotingObjects {public interface WriteFileToLocal {void write (string content);} public class Process: externalbyrefobject, WriteFileToLocal {public Process () {Console. writeLine ("Write Starting... ");} /// <summary> /// write File /// </summary> /// <param name = "content"> write File content </param> public void write (string content) {using (StreamWriter sw = new StreamWriter (@ "D: \ remoting.txt", true, Encoding. default) {sw. write (content + "\ r \ n ");}}}}RemotingObjects