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
(http://support.microsoft.com/kb/818780/
)
HOW TO: Create a remote server by using Visual C++ .NET
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:
#using <System.Runtime.Remoting.dll>
Use the following code to add a reference to the ServerClass.dll assembly that you created in
818780
(http://support.microsoft.com/kb/818780/EN-US/
)
:
#using <ServerClass.dll>
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.
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.
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.
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.
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.
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;
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:
Click Project, and then click
<ProjectName> Properties.
Note <ProjectName> 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.
For more information about the common language runtime
support compiler option, visit the following Microsoft Web site:
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:
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
(http://support.microsoft.com/kb/818780/EN-US/
)
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,"tcp://localhost:8085/RemoteTest"));
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.");
Use the ReadLine method of the Console object to keep the client application running.
System::Console::WriteLine("Hit <enter> to exit...");
System::Console::ReadLine();
Build your project.
Make sure that the server application is running. (This is
the .exe file that you created in
818780
(http://support.microsoft.com/kb/818780/EN-US/
)
.)
Copy ServerClass.dll to the directory where ClientApp.Exe resides
Run the project, and then test the client-to-server
communication.