Easy from remote C-S system

zhaozj2021-02-17  52

This article will show you how to easily stand up to the remote client / server architecture, let you experience the unlimited streamlined convenience brought by C #. First, implement the server side. The code analysis is as follows: // introduces the corresponding namespace Using System; use system.Runtime.Remting.Runting.Channels; use system.Runtime.Remoting.Channels.Runtime.Remoting.Channels.TCP;

Namespace ServerClass {// Implement a server and client to communicate class MyRemoteClass Public Class MyRemoteClass Public Class MyRemoteClass: MarshalByRefObject {public myRemoteClass () {} // This method is to communicate with the client, of course, you can also define more more Method // The client passes a string that the public bool setString (String STEMP) {Try {// server-side prints are printed. Return logic value console.writeline ("this string '{0}', step, step.Length); Return STEMP! =" ";} CatCh}}}}

// Server Control Class, this class is just to control the role of the startup and shut down the server, you can also put it in the MyRemoteClass class. public class MyServer {public static void Main () {// Open and sign up for a service TcpChannel chan = new TcpChannel (8085); ChannelServices.RegisterChannel (chan); RemotingConfiguration.RegisterWellKnownServiceType (System.Type.GetType ( "ServerClass.MyRemoteClass") "Remotetest", WellknownObjectMode.singlecaLL); // Keep running System.Console.writeline ("Hit to EXIT ..."); system.console.readline ();}}}

Then, the client is implemented. The code analysis is as follows: // introduces the corresponding namespace Using System; use system.Runtime.Reming.Channels; use system.runtime.remoting.channels.tcp; // introduce the server and client to communicate class MyRemoteClassusing ServerClass;

namespace ClientClass {public class MyClient {public static void Main () {try {// open a TCP connection and register TcpChannel chan = new TcpChannel (); ChannelServices.RegisterChannel (chan); connected to the server, acquiring communications MyRemoteClass obj = (MyRemoteClass ) Activator.getObject (TyremoteClass), "TCP: // localhost: 8085 / transtetest"); if (obj == null) System.Console.writeline ("Could Not Locate Server"); Else IF (Obj.setstring ("Sending String to Server"))) System.Console.writeline ("Success: Check The Other Console To Verify); Else System.Console.writeline (" Sending The Test String Has Failed. "); System.Console. WriteLine ("Hit to EXIT ..."); system.console.readline ();} catch (exception exp) {console.writeLine (exp.stacktrace);}}}} Compile server code CSC CSC / OUT : Myserver.exe myserver.cs Compile client code CSC /R: MYSERVER.EXE MyClient.cs Starting Server C: /> Start MyServer Start Client C: /> MyClient.net puts a lot of function packages too good, to programmers Brought a lot of convenience, this paper only involves one of the specific applications. In the wrong place, please contact ZlyPerson@163.net

转载请注明原文地址:https://www.9cbs.com/read-29355.html

New Post(0)