Microsoft KB Archive/818781

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

Article ID: 818781

Article Last Modified on 5/16/2007

-

APPLIES TO


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

-





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



For a Microsoft Visual Basic .NET version of this article, see 300943.

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
 * How to Create a Client to a Remote Server
 * REFERENCES



SUMMARY
This step-by-step article describes how to create a client that accesses a remote server. This client can be located on the same computer, on a different computer, or on a different network. This article builds on the following Microsoft Knowledge Base article:

818780 HOW TO: Create a remote server by using Visual C++ .NET

back to the top



Requirements
This list outlines the recommended hardware, software, network infrastructure, and service packs that you must have:
 * Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
 * 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

You must also build applications that are described in the following article:

818780 HOW TO: Create a remote server by using Visual C++ .NET

back to the top

How to Create a Client to a Remote Server
 In Visual C++ .NET or in Visual C++ 2005, create a new Managed C++ Console Application project. Name the project ClientApp .  Open the code file ClientApp.cpp. Use the following code to add a reference to 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 818780:
 * 1) using  
 * 1) using 

Note To specify the assembly search path, follow these steps:  In the Property Pages dialog box of the project, open the C/C++ folder, and then click the General tab. Modify the Resolve #using References property to the folder where the ServerClass.dll assembly exists.</li></ol>

There are three different methods for a client to reference remote objects, and each of these is resolved at compile time. This example uses Method 1. <ul> Method 1: Compile the server object, and specify the .exe or .dll file as a reference to the compiler when you compile the client. This method is useful when both the client and server components are developed at the same site.</li> Method 2: Derive the server object from an interface class, and compile the client with the interface. This method is useful when the client and server components are not developed at the same site. The interface(s) may be compiled to a .dll, and then shipped to the client sites as necessary. Avoid changing a published interface.</li> Method 3: Use the SoapSUDS tool to extract the required metadata from a running server object. This method is useful when client and server components are developed at different sites, and when no interface classes are available. Point the SoapSUDS tool at a remote Uniform Resource Identifier (URI), and then generate the required metadata as source or as a .dll. The SoapSUDS tool only extracts metadata, and does not generate the source for the remote object.</li></ul> </li>  Use the using namespace statement on the Remoting namespace, the Remoting.Channels namespace, and the Remoting.Channels.TCP namespace, and on the ServerClass classname so that you do not have to qualify declarations in those namespaces later in your code. You must use the using statement in front of any other declarations. using namespace System; using namespace System::Runtime::Remoting; using namespace System::Runtime::Remoting::Channels; using namespace System::Runtime::Remoting::Channels::Tcp; using namespace ServerClass; </li>  Declare a variable to initialize a TcpChannel object that the client uses to connect to the server application. 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; ChannelServices::RegisterChannel(chan); 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: <ol style="list-style-type: lower-alpha;"> Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> 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> Declare and instantiate the remote server. In this example, use the GetObject method of the Activator object to create an instance of the myRemoteClass object, and then specify the following parameters: <ul> The type of the well-known object where you want to connect. In this sample, you will connect to the ServerClass.myRemoteClass object that you created in 818780</li>  Activate the URI of the object. The URI must include the protocol (TCP), the computer name (localhost), the port (8085), and the endpoint of the server object (RemoteTest). To access the ServerClass remote server that is located on the local server, use the following URI:

tcp://localhost:8085/RemoteTest

Type * objType = __typeof(myRemoteClass); myRemoteClass* obj = static_cast<myRemoteClass*>( Activator::GetObject(objType,&quot;tcp://localhost:8085/RemoteTest&quot;)); if (obj == NULL) System::Console::WriteLine(&quot;Could not locate server&quot;); else if (obj->SetString(&quot;Sending String to Server&quot;)) System::Console::WriteLine(&quot;Success: Check the other console to verify.&quot;); else System::Console::WriteLine(&quot;Sending the test string has failed.&quot;); </li>  Use the ReadLine method of the Console object to keep the client application running. System::Console::WriteLine(&quot;Hit to exit...&quot;); System::Console::ReadLine; </li></ul> </li> Build your project.</li> Make sure that the server application is running. (This is the .exe file that you created in 818780.)</li> Copy ServerClass.dll to the directory where ClientApp.Exe resides</li> <li>Run the project, and then test the client-to-server communication.</li></ol>

back to the top

<div class="references_section">