Microsoft KB Archive/818780

= How to create a remote server by using Visual C++ .NET or Visual C++ 2005 =

Article ID: 818780

Article Last Modified on 8/28/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0
 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-







For a Microsoft Visual C# .NET version of this article, see 307445.



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.Remoting.Channels
 * System.Runtime.Remoting.Channels.Tcp

IN THIS TASK

 * SUMMARY
 * Requirements
 * Create the Remote Server Object
 * Create the Remote Server Application
 * Test the Server Object
 * REFERENCES



SUMMARY
This step-by-step article describes how to create a simple, remote server that another application can access. The application that accesses the server can be located on the same computer, on a different computer, or on a different network. The remote server is divided in two parts:
 * Server object
 * 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 .NET Framework remoting.

back to the top



Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you will need:
 * Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
 * The Microsoft .NET Framework

This article assumes that you are familiar with the following topics:
 * Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
 * Microsoft Visual C++ .NET or Microsoft Visual C++ 2005
 * Networking

back to the top

Create the Remote Server Object
To create the server application, you must first create a server object. The server object is what the client application instantiates, and then uses to communicate with the server computer. The client application does this through a proxy object that is created on the client. In this example, the server object resides in a Class Library (.dll), and is named myRemoteClass.  Create a new Managed C++ Class Library in Visual C++ .NET or in Visual C++ 2005. Name the project ServerClass. By default, Class1 is created.  Open ServerClass.h, and then rename Class1 to myRemoteClass.

The renamed myRemoteClass must inherit from the MarshalByRefObject class. Your class now appears as follows: namespace ServerClass { public __gc class myRemoteClass: public MarshalByRefObject {   public: myRemoteClass {   }  }; }  Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:  Click Project, and then click  Properties.

Note  is a placeholder for the name of the project. Expand Configuration Properties, and then click General. Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

</li>  Add a public method to myRemoteClass that takes a string, displays a message to the console with a value of the string, and then returns True if the string is not empty. bool SetString(String* sTemp) { try {   Console::WriteLine(&quot;This string '{0}' has a length of {1}&quot;, sTemp, (sTemp->get_Length).ToString); return (sTemp->get_Length != 0? true:false) ; } catch(Exception* ex) {   Console::WriteLine(ex->Message); return false; } } </li> Build the project to create the ServerClass.dll assembly.</li> Save and then close the project.</li></ol>

back to the top

Create the Remote Server Application
After you create the server object that the client communicates with, register this object with .NET Framework remoting. When you register the object, you must also start the server, and then make sure that the server listens on a port for clients to connect to that port. To do this, you must use a project type that outputs an executable file.

Include the server object in a separate project so that you can easily refer the server object from the client project. If you include the server object in this project, you cannot refer because references can only be set to .dll files. <ol> Create a new Console Application project in Visual C++ .NET or a new CLR Console Application in Visual C++ 2005 to start the remote server. Name the project ServerObjectRef .</li>  Open the ServerObjectRef.cpp code file. Use the following code to add a reference to the System namespace and the System.Runtime.Remoting namespace to the project:  Use the following code to add a reference to the ServerClass.dll assembly that you created in the previous section: Note To specify the assembly search path, follow these steps: <ol style="list-style-type: lower-alpha;"> Open the Property Pages dialog box of the project.</li> Open the C/C++ folder.</li> Click the General tab.</li> Modify the Resolve #using References property to the folder where the ServerClass.dll assembly exists.</li></ol> </li>  Use the USING statement on the Remoting namespace, the Remoting.Channels namespace, and the Remoting.Channels.TCP namespace so that you do not have to qualify declarations in those namespaces later in your code. You must use the USING statement before any other declarations. using namespace System::Runtime; using namespace System::Runtime::Remoting; using namespace System::Runtime::Remoting::Channels; using namespace System::Runtime::Remoting::Channels::Tcp; </li>  Declare the appropriate variable. Declare, and then initialize a TcpChannel object that listens for clients to connect on a certain port. In this example, it is port 8085. Use the RegisterChannel method to register the channel with the channel services. Add the following declaration code in the main function: TcpChannel* chan = new TcpChannel(8085); ChannelServices::RegisterChannel(chan); </li> Call the RegisterWellKnownServiceType method of the RemotingConfiguration object to register the ServerClass object with .NET Framework remoting. Specify the following parameters: <ul> Specify the type of the object that is registered as a well-known type. In this sample, the __typeof keyword is used to get the type of ServerClass.myRemoteClass.</li> Name the endpoint where the object will be published as RemoteTest. Clients must know this name to connect to the object.</li>  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. When a SingleCall object is used, 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 one time. Conversely, Singleton objects are created only one time, and all clients communicate with the same object. Type * objType = __typeof(ServerClass); RemotingConfiguration::RegisterWellKnownServiceType( objType,    &quot;RemoteTest&quot;,     WellKnownObjectMode::SingleCall); </li></ul> </li> <li> Use the ReadLine method of the Console object to keep the server application running. System::Console::WriteLine(&quot;Hit to exit...&quot;); System::Console::ReadLine; </li> <li>Build your project.</li> <li>Save, and then close your project.</li></ol>
 * 1) using <System.Dll>
 * 2) using <System.Runtime.Remoting.Dll> </li>
 * 1) using <ServerClass.dll>

back to the top

Test the Server Object
For 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:

818781 HOW TO: Create client access to a remote server by using Visual C++ .NET

back to the top

<div class="references_section">