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



The information in this article applies to:

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)
  • 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

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

  1. In Visual C++ .NET or in Visual C++ 2005, create a new Managed C++ Console Application project. Name the project ClientApp.
  2. 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>
  3. Use the following code to add a reference to the ServerClass.dll assembly that you created in 818780:
    #using <ServerClass.dll>

    Note To specify the assembly search path, follow these steps:
    1. In the Property Pages dialog box of the project, open the C/C++ folder, and then click the General tab.
    2. 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.
  4. 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;
    
  5. 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:
    1. Click Project, and then click <ProjectName> Properties.

      Note <ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. 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:

    /clr (Common Language Runtime Compilation)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

  6. 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
    • 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();
      
  7. Build your project.
  8. Make sure that the server application is running. (This is the .exe file that you created in 818780.)
  9. Copy ServerClass.dll to the directory where ClientApp.Exe resides
  10. Run the project, and then test the client-to-server communication.
back to the top

REFERENCES

For more information about the TcpChannel class, visit the following Microsoft Developer Network (MSDN) Web site:For an introduction to remoting in the .NET Framework, visit the following MSDN Web site:For more information, visit the following MSDN Web site:For an overview of Remoting, see the Microsoft .NET Framework Developer's Guide.

For more information about the Activator.GetObject and Type.GetType methods, see the .NET Framework Class Library documentation.

back to the top

Modification Type:MajorLast Reviewed:1/4/2006
Keywords:kbHOWTOmaster kbRemoting kbTunneling kbhowto KB818781 kbAudDeveloper