How to: Create a remote server using Microsoft Visual C # .NET (from MSDN)

xiaoxiao2021-03-06  75

This Article Was previously Published Under Q307445

For a Microsoft Visual Basic .NET VERSION OF This Article, See

300951.

This Article Refers To The Following Microsoft .NET Framework Class Library Namespaces:

System.Runtime.Remoting System.Runtime.Runting.Channels System.Runtime.Remoting.Channels.tcp

In this task

Summary

Requirements Creating The Remote Server Object Creating The Remote Server Application Testing The Server Object References

SUMMARYThis article illustrates how to create a simple, remote server that another application can access. The application that accesses this server can be located on the same computer, on a different computer, or on a different network. The remote server is broken into two parts :. the server object and the server application The server object is the object that the client communicates with, and the server application is used to register the server object with the Remoting run-time framework.

Back to the top

RequirementSthe Following List Outlines The Recommended Hardware, Software, Network Infrastructure, And Service Packs That You Will NEED:

Visual Studio .Netthis Article Assumes That You Are Familiar with The Following Topics:

Visual Studio .NET Visual C # .NET Networking

Back to the top

Creating the remote server objectThe first step in creating the server application is to create a server object. The server object is what the client application instantiates and communicates with on the server computer. The client application does this through a proxy object that is created on the Client. In this Sample, The Server Object Resides in a class library (dll) and is ca

MyRemoteClass.

Create a new Class Library application in Visual C # .NET. Name the project ServerClass. Class1 is created by default. In Solution Explorer, rename the Class1.cs code file to ServerClass.cs. Open ServerClass.cs and rename Class1 to myRemoteClass. You also need to rename the default constructor for the class to so that it matches the class name myRemoteClass should inherit from the MarshalByRefObject class Your class should now appear as follows:.. public class myRemoteClass: MarshalByRefObject {

Public myremoteclass ()

{

//

// to do: add constructor logic here.

//

}

}

Add a public method to myremoteclass what take the console with a value of the string, and return.public bool setstring (String STEMP)

{

Try

{

Console.writeline ("this string '{0}' Has a Length of {1}", STEMP, STEMP.LENGTH;

Return STEMP! = ""

}

Catch

{

Return False;

}

}

Build The Project To Create The ServerClass.dll Assembly. Save and Close The Project.

Back to the top

Creating the remote server applicationAfter you have created the server object that the client will communicate with, you must register this object with the Remoting framework. When you register the object, you must also start the server and have the server listen on a port for clients TO Connect To That Port. To do this, you need...............

The reason to include the server object in a separate project is so that you can easily reference the server object from the client project. If you include it in this project you can not reference it, because references can only be set to DLL files.

Create a new Console Application in Visual C # .NET to start the remote server. Class1 is created by default. In Solution Explorer, rename the Class1.cs file to RemoteServer.cs. Add a reference to the System.Runtime.Remoting namespace. Add a reference to the ServerClass.dll assembly that you created in the previous section. Use the using statement on the Remoting, Remoting.Channels, and Remoting.Channels.TCP namespaces so that you are not required to qualify declarations in those namespaces later in your You Must Use The Using Statement Prior To Any Other Declarations.Using System.Runtime.remoting; Using System.Runtime.Remting.Channels;

Using system.runtime.remoting.channels.tcp;

Declare the appropriate variable. Declare and initialize a TcpChannel object that listens for clients to connect on a certain port, which is port 8085 in this example. Use the RegisterChannel method to register the channel with the channel services. Add the following declaration code in the Main procedure of class1: tcpchannel chan = new tcpchannel (8085);

ChannelServices.RegisterChannel (CHAN);

Call the registerwellknowntype method of the remotingconfiguration Object to register the serverclass Object with the remoting framework, and specify the following parameters:

The full type name of the object that is being registered (which is ServerClass.myRemoteClass in this example), followed by the assembly name ServerClass. Specify both the name of the namespace as well as the class name here. Because you did not specify a namespace in the previous section, the default root namespace is used. name the endpoint where the object is to be published as RemoteTest. Clients need to know this name in order to connect to the object. Use the SingleCall object mode to specify the final parameter . The object mode specifies the lifetime of the object when it is activated on the server. In the case of SingleCall objects, a new instance of the class is created for each call that a client makes, even if the same client calls the same method more than once. On the other hand, Singleton objects are created only once, and all clients communicate with the same object.RemotingConfiguration.RegisterWellKnownServiceType (System.Type.GetType ( "ServerClass.myRemoteClass, ServerC Lass "),

"Remotetest",

WellknownObjectMode.singlecAl1;

Use the readline method of the console object to keep the server application running. System.console.writeline ("Hit To EXIT ...");

System.console.readline ();

Build Your Project. Save and close the project.

Back to the top

Testing the server objectFor additional information about how to create a client application that communicates with the server object that you just created, click the following article number to view the article in the Microsoft Knowledge Base:

300943 HOW to Create Client Access To Remote Server by Using Visual Basic .NET

Back to the top

Referencesfor Additional Information, Click The Following Article Numbers to View The Articles in The Microsoft Knowledge Base: 301116 How To Marshal An Object To a Remote Server by Reference by Using Visual Basic .NET

301112 How to Marshal An Object To A Remote Server by Reference By Using Visual Basic .NET for AdditionAl

TCPChannel Class, Visit The Following Microsoft Developer Network (MSDN) Web Site:

Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemRuntimeremotingChannelStcptcPchannelTopic.aspfor AdditionAnelClasstopic.aspfor Additional Information About

RemotingConfiguration.registerwellknownServiceType Method, Visit The Following MSDN Web Site:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemRuntimeRemotingRemotingConfigurationClassRegisterWellKnownServiceTypeTopic.aspFor additional information about Quickstart Tutorials, visit the following gotodotnet.com Web site:

Www.gotodotnet.com/quickstartfor an overview of .NET Remoting, see. NET Framework Developer's Guide Documentation.

For Additional Information About MSDN Discussion Groups and Visual C # .NET from The USEnet NewsGroup, Visit The Following Microsoft Web Site:

Microsoft.public.dotNet.Languages.csharp

Back to the top

The Information in this Article Applies TO:

Microsoft Visual C # .NET (2002)

Last Reviewed: 7/12/2004 (2.1) Keywords: Kbchannels KbhowTomaster Kbtunneling KB307445 KBAUDDEVELOPER

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

New Post(0)